Casting interface implemented

This commit is contained in:
Vinicius Silva 2024-01-22 22:36:12 -03:00
parent b800b3c07b
commit a2513b3633
2 changed files with 42 additions and 1 deletions

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

@ -1,4 +1,4 @@
module main
interface Foo{
write(string) string