Gopherjs: Broken type assertion with structs embedding

Created on 19 Feb 2018  路  3Comments  路  Source: gopherjs/gopherjs

The $assertType helper only considers the method set when asserting if a type implement an interface. But with struct embedding, it is possible for a struct field to shadow a method.

This fails gopherjs run:

package main

type Doer interface {
    Do()
}

type Embedded struct{}

func (e *Embedded) Do() {}

type A struct {
    Embedded
    Do string
}

func main() {
    var a interface{}
    a = &A{}
    if aa, ok := a.(Doer); ok {
        aa.Do()
    }
}

It incorrectly considers a a Doer and ends up failing with:

TypeError: aa.Do is not a function

Most helpful comment

If anyone else was confused (as I was :), the point is not that aa.Do() should call A.Embedded.Do(), the point is that the type assertion should've failed: a is not actually a Doer:

// In the regular Go playground
if aa, ok := a.(Doer); ok {
    aa.Do()
} else {
    println("not a doer")
}
// => not a doer

All 3 comments

If anyone else was confused (as I was :), the point is not that aa.Do() should call A.Embedded.Do(), the point is that the type assertion should've failed: a is not actually a Doer:

// In the regular Go playground
if aa, ok := a.(Doer); ok {
    aa.Do()
} else {
    println("not a doer")
}
// => not a doer

@theclapp That's correct, I updated to the description in an effort to make that clearer.

So, is $assertType wrong, and needs to look into properties of the type too, or should this method not be in the methodset of the type to begin with?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

freeozyl80 picture freeozyl80  路  3Comments

hajimehoshi picture hajimehoshi  路  6Comments

nytehauq picture nytehauq  路  7Comments

myitcv picture myitcv  路  6Comments

flimzy picture flimzy  路  4Comments