V version: V 0.1.21 99e0771
OS: macOS 10.14.6
What did you do?
interface Reader {
name() string
}
fn foo(arr []Reader) {
}
What did you expect to see?
I can pass an array of structs that implement the interface to foo().
What did you see instead?
error: subscript of pointer to incomplete type 'Reader' (aka 'struct Reader')
Reader arr = ((Reader *) tmp5 . ...
(Use `v -g` to print the entire error message)
V error: C error. This should never happen. Please create a GitHub issue
That's not a bug. Array of interfaces are not implemented yet. WIP
Well that's still a bug :)
I've been playing around with V recently (I really really like it by the way! 馃槃) and was hunting for this...
I came across this blog post with the syntax for how it's implemented in Go - http://golangtutorials.blogspot.com/2011/06/polymorphism-in-go.html
type Vehicle interface {
move() string
}
type Car struct {
}
type Plane struct {
}
func (c Car) move() string {
return "drive"
}
func (p Plane) move() string {
return "fly"
}
p := new (Plane)
c := new (Car)
vehicles := [...]Vehicle{p, c}
I was trying to attempt something like this in V but wasn't able to - glad this is just something that's still in the works. Is this on a road map anywhere? Do you imagine it working in a similar way?
Keep up the awesome work! 馃憤
Yes, interfaces are broken right now. They will be fixed in early November.
Awesome, thanks for the information!
It was partially fixed.
Here is an example where the issue is still present:
Summary (Click to expand)
struct Dog {
breed string
}
fn (d Dog) name() string {
return 'Dog'
}
fn main() {
dog := Dog{}
// get_name(dog) // uncommenting this line makes the example working
get_names([dog, dog])
}
fn get_names(speakers []Speaker) {
for s in speakers {
println(s.name())
}
}
interface Speaker {
name() string
}
The last example is fixed in 8caf382 . The first one is also no longer a problem.
Most helpful comment
The last example is fixed in 8caf382 . The first one is also no longer a problem.