Interfaces in V

Posted by yhuang
Last Edited by dhonx
Public (Editable by Users)
V
None
Edit
// A type implements an interface by implementing its methods.
// There is no explicit declaration of intent, no "implements" keyword.

struct Dog {}
struct Cat {}

fn (d Dog) speak() string {
	return 'woof'
}

fn (c Cat) speak() string {
	return 'meow' 
}

interface Speaker {
	speak() string
}

fn perform(s Speaker) {
	// TODO    
    str := s.speak()
	println(str)
}

dog := Dog{}
cat := Cat{}
perform(dog) // "woof" 
perform(cat) // "meow"