Merge pull request #15 from viniciusfdasilva/dev

Enuns and interfaces created
This commit is contained in:
Vinicius Silva 2024-01-22 22:37:16 -03:00 committed by GitHub
commit f918fe3cbb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 209 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 { 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)
} }

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

41
16_interfaces/casting.v Normal file
View File

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

View File

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

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