I found a bug while playing Vlang playground.
Steps to reproduce:
interface Speaker {
speak() string
}
struct Foobar{}
fn (f mut Foobar) speak() string {
return 'I am foobar'
}
fn printSpeaker(s Speaker) {
println(s.speak())
}
fn main() {
// I think f's type is pointer to Foobar.
// (and I assumed that f implement Speaker interface)
f := &Foobar{}
// I assumed like above, then I think f can be passed as printSpeaker()'s argument.
printSpeaker(f)
}
You just found a bug. V can't compile this program, but it should. Please create a GitHub issue.(Maybe my English is something wrong. sorry.)
Thanks for reporting, this will take a bit more time to fix. Interfaces don't work with mutable receivers right now.
Looks like error raises not only with pointers. This code works well:
interface Speaker {
speak() string
}
struct Foobar{}
fn (f Foobar) speak() string{
return 'I am foobar'
}
fn main() {
f := Foobar{}
println(f.speak())
}
but this one dont:
interface Speaker {
speak() string
}
struct Foobar{}
fn (f Foobar) speak() string{
return 'I am foobar'
}
fn print_speaker(s Speaker) {
println(s.speak())
}
fn main() {
f := Foobar{}
print_speaker(f)
}
Extension of #73.