v/vlib/compiler/tests/interface_test.v

46 lines
561 B
V
Raw Normal View History

struct Dog {
2019-09-04 17:49:46 +02:00
breed string
}
2019-11-08 04:03:06 +01:00
struct Cat {
breed string
}
2019-11-08 04:03:06 +01:00
fn (d Cat) name() string { return 'Cat' }
fn (d Cat) speak() { println('meow') }
fn (d Dog) speak() { println('woof') }
fn (d Dog) name() string { return 'Dog'}
interface Speaker {
name() string
2019-08-31 15:38:13 +02:00
speak()
}
interface Speak2er {
name() string
2019-11-08 04:03:06 +01:00
speak()
}
2019-11-08 04:03:06 +01:00
struct Foo {
speaker Speaker
}
fn perform_speak(s Speaker) bool {
s.speak()
return true
}
fn test_perform_speak() {
d := Dog{}
assert perform_speak(d)
2019-11-08 04:03:06 +01:00
cat := Cat{}
assert perform_speak(cat)
f := Foo {
//speaker: d
}
}