V: Cannot compile program contains interface that implemented by pointer type

Created on 4 Apr 2019  路  3Comments  路  Source: vlang/v

I found a bug while playing Vlang playground.

Steps to reproduce:

  1. Enter program list:
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)
}
  1. Click Run Button
  2. I got output as below:
    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.)

Bug

All 3 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

radare picture radare  路  3Comments

jtkirkpatrick picture jtkirkpatrick  路  3Comments

taojy123 picture taojy123  路  3Comments

elimisteve picture elimisteve  路  3Comments

ArcDrake picture ArcDrake  路  3Comments