Enuns and interfaces created
This commit is contained in:
parent
a4c4c4486c
commit
7a5d384d66
Binary file not shown.
|
|
@ -0,0 +1,38 @@
|
||||||
|
|
||||||
|
|
||||||
|
type Filter = fn (string) string
|
||||||
|
|
||||||
|
|
||||||
|
fn filter(s string, f Filter) string{
|
||||||
|
return f(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn uppercase(s string) string{
|
||||||
|
return s.to_upper()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
type String = string
|
||||||
|
|
||||||
|
|
||||||
|
fn (s String) my_upper() string{
|
||||||
|
return s.to_upper()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main(){
|
||||||
|
|
||||||
|
res := filter('Teste', fn (s string) string {
|
||||||
|
return s
|
||||||
|
})
|
||||||
|
|
||||||
|
println(res)
|
||||||
|
|
||||||
|
println(uppercase('teste'))
|
||||||
|
|
||||||
|
mut str := String('oi')
|
||||||
|
println(str)
|
||||||
|
println(str.my_upper())
|
||||||
|
|
||||||
|
my_filter := Filter(uppercase)
|
||||||
|
println(my_filter)
|
||||||
|
}
|
||||||
|
|
@ -5,32 +5,68 @@ enum Color as u8{
|
||||||
blue
|
blue
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn main(){
|
|
||||||
|
|
||||||
|
enum Grocery{
|
||||||
|
apple
|
||||||
|
orange = 5
|
||||||
|
pear
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
enum Cycle{
|
||||||
|
one
|
||||||
|
two
|
||||||
|
three
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn (c Cycle) next() Cycle{
|
||||||
|
|
||||||
|
match c {
|
||||||
|
.one {
|
||||||
|
return .two
|
||||||
|
}
|
||||||
|
|
||||||
|
.two {
|
||||||
|
return .three
|
||||||
|
}
|
||||||
|
|
||||||
|
.three {
|
||||||
|
return .one
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn main(){
|
||||||
mut color := Color.red
|
mut color := Color.red
|
||||||
|
|
||||||
color = .green
|
color = .green
|
||||||
|
|
||||||
mut color1 := Color.@none
|
|
||||||
|
|
||||||
println(color)
|
println(color)
|
||||||
|
|
||||||
match color {
|
match color {
|
||||||
.red {
|
.red { println('The color was red') }
|
||||||
println('the color was red')
|
.green { println('The color was green')}
|
||||||
|
.blue { println('The color was blue ')}
|
||||||
|
else { println('The value is none')}
|
||||||
}
|
}
|
||||||
|
|
||||||
.green {
|
color = Color.@none
|
||||||
println('the color was green')
|
println(color)
|
||||||
|
|
||||||
|
|
||||||
|
g1 := int(Grocery.apple)
|
||||||
|
g2 := int(Grocery.orange)
|
||||||
|
g3 := int(Grocery.pear)
|
||||||
|
|
||||||
|
println('Grocery ids ${g1}, ${g2}, ${g3}')
|
||||||
|
|
||||||
|
|
||||||
|
mut c := Cycle.one
|
||||||
|
|
||||||
|
for _ in 0..10 {
|
||||||
|
println(c)
|
||||||
|
c = c.next()
|
||||||
}
|
}
|
||||||
|
|
||||||
.blue {
|
|
||||||
println('the color was blue')
|
|
||||||
}
|
|
||||||
|
|
||||||
else {
|
|
||||||
println('color was not indentified')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
println(color1)
|
|
||||||
}
|
}
|
||||||
|
|
@ -0,0 +1,12 @@
|
||||||
|
|
||||||
|
type String = string
|
||||||
|
|
||||||
|
fn (s String) my_upper() string {
|
||||||
|
return s.to_upper()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main(){
|
||||||
|
|
||||||
|
mut str := String('abc')
|
||||||
|
println(str.my_upper())
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,34 @@
|
||||||
|
struct Dog{
|
||||||
|
breed string
|
||||||
|
}
|
||||||
|
|
||||||
|
fn (d Dog) speak() string{
|
||||||
|
return 'woof'
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Cat{
|
||||||
|
breed string
|
||||||
|
}
|
||||||
|
|
||||||
|
fn (c Cat) speak() string{
|
||||||
|
return 'meow'
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Speaker{
|
||||||
|
breed string
|
||||||
|
speak() string
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main(){
|
||||||
|
dog := Dog{'Leonberger'}
|
||||||
|
cat := Cat{'Siamese'}
|
||||||
|
|
||||||
|
mut arr := []Speaker{}
|
||||||
|
|
||||||
|
arr << dog
|
||||||
|
arr << cat
|
||||||
|
|
||||||
|
for item in arr{
|
||||||
|
println('a ${item.breed} says: ${item.speak()}')
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue