On 11/13/13 3:57 AM, Kevin Ballard wrote:
On Nov 11, 2013, at 11:43 PM, Patrick Walton <[email protected]
<mailto:[email protected]>> wrote:

We considered Go's anonymous fields but rejected them because they
don't support virtual methods at the same time as field embedding.

I don’t follow. Why do Go’s anonymous fields not support virtual methods
at the same time as field embedding?

I want to write this but I can't:

    package main

    import "fmt"

    type A struct {
        x int
    }

    type B struct {
        A
        y int
    }

    type C struct {
        A
        z int
    }

    func(self *B) Foo() {
        fmt.Printf("Hello!")
    }

    func(self *B) Upcast() *A {
        return &self.A
    }

    func(self *C) Foo() {
        fmt.Printf("Goodbye!")
    }

    func(self *C) Upcast() *A {
        return &self.A
    }

    func main() {
        myArray := []*A {
            (&B { A: A { x: 1 }, y: 2 }).Upcast(),
            (&C { A: A { x: 3 }, z: 4 }).Upcast(),
        }
        for i := 0; i < 2; i++ {
            myArray[i].Foo()
        }
    }

Error:

prog.go:41: myArray[i].Foo undefined (type *A has no field or method Foo)

You can't really write the thing you need to write inside `Upcast` to make this code work. You would need to use an interface instead, but then interfaces don't support shared fields, just like in Rust, leading to the same problem.

Patrick

_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to