diff --git a/02_read/main b/02_read/main new file mode 100755 index 0000000..8c75e43 Binary files /dev/null and b/02_read/main differ diff --git a/15_enums/function_types.v b/15_enums/function_types.v new file mode 100644 index 0000000..239770b --- /dev/null +++ b/15_enums/function_types.v @@ -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) +} \ No newline at end of file diff --git a/15_enums/main.v b/15_enums/main.v index 1724b1f..c25b226 100644 --- a/15_enums/main.v +++ b/15_enums/main.v @@ -1,36 +1,72 @@ -enum Color as u8{ +enum Color as u8 { @none red green 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 + color = .green - - mut color1 := Color.@none - println(color) match color { - .red { - println('the color was red') - } - - .green { - println('the color was green') - } - - .blue { - println('the color was blue') - } - - else { - println('color was not indentified') - } + .red { println('The color was red') } + .green { println('The color was green')} + .blue { println('The color was blue ')} + else { println('The value is none')} } - println(color1) -} + color = Color.@none + 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() + } + +} \ No newline at end of file diff --git a/15_enums/type.v b/15_enums/type.v new file mode 100644 index 0000000..605500e --- /dev/null +++ b/15_enums/type.v @@ -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()) +} \ No newline at end of file diff --git a/15_enums/types_declarations.v b/15_enums/types_declarations.v deleted file mode 100644 index e69de29..0000000 diff --git a/16_interfaces/casting.v b/16_interfaces/casting.v new file mode 100644 index 0000000..7b4c443 --- /dev/null +++ b/16_interfaces/casting.v @@ -0,0 +1,41 @@ + + +struct Dog{ + breed string + speak() string +} + +fn (d Dog) speak() string { + println('Woof') +} + + +struct Cat{ + breed string + speak() string +} + +fn (c Cat) speak() string{ + println('Meow') +} + +interface Something{} + + +fn announce(s Something){ + if s is Dog { + println('a ${s.breed] dog') + }else if s is Cat{ + println('a cat speaks ${s.speak()}') + }else { + println('something else') + } +} + +fn main(){ + dog := Dog{'Leonberger'} + cat := Cat{'Siamese'} + + announce(dog) + announce(cat) +} \ No newline at end of file diff --git a/16_interfaces/interfaces.v b/16_interfaces/interfaces.v new file mode 100644 index 0000000..4f173de --- /dev/null +++ b/16_interfaces/interfaces.v @@ -0,0 +1,26 @@ + + +interface Foo{ + write(string) string +} + +interface Bar{ +mut: + write(string) string +} + +struct MyStruct{} + +fn (s MyStruct) write(a string) string{ + return a +} + +fn main(){ + s1 := MyStruct{} + + fn1(s1) +} + +fn fn1(s Foo){ + println(s.write('Foo')) +} \ No newline at end of file diff --git a/16_interfaces/main.v b/16_interfaces/main.v new file mode 100644 index 0000000..267e630 --- /dev/null +++ b/16_interfaces/main.v @@ -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()}') + } +} \ No newline at end of file