Enuns and interfaces created

This commit is contained in:
Vinicius Silva 2024-01-22 22:16:49 -03:00
parent a4c4c4486c
commit 7a5d384d66
6 changed files with 142 additions and 22 deletions

BIN
02_read/main Executable file

Binary file not shown.

38
15_enums/function_types.v Normal file
View File

@ -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)
}

View File

@ -1,36 +1,72 @@
enum Color as u8{ enum Color as u8 {
@none @none
red red
green green
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 { }
println('the color was green')
} color = Color.@none
println(color)
.blue {
println('the color was blue')
} g1 := int(Grocery.apple)
g2 := int(Grocery.orange)
else { g3 := int(Grocery.pear)
println('color was not indentified')
} println('Grocery ids ${g1}, ${g2}, ${g3}')
mut c := Cycle.one
for _ in 0..10 {
println(c)
c = c.next()
} }
println(color1)
} }

12
15_enums/type.v Normal file
View File

@ -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())
}

34
16_interfaces/main.v Normal file
View File

@ -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()}')
}
}