Say I have a

type Node interface {
    Less(Node) bool
}


and

type Element struct {
    value int
}


func callLess(n1, n2 Node) {
    n1.Less(n2)
}


I understand that type-assertions are generally required to get a different 
type from an interface. But in this specific case where a type is known to 
implement the interface, I am confused with the language's behavior. For 
example,

func (e E) Less(n Node) bool {
    return e.value < n.(E).value
}


satisfies the interface, but requires a type assertion. However,

func (e E) Less(e2 Element) bool {
    return e.value < e2.value
}


generates a compile-time error. The question is, why doesn't the second 
Less method satisfy Node, if Element (the parameter) implements Node? If 
struct-types are auto converted into interfaces in a parameter, for e.g.

acceptsReader := func(r io.Reader) {}
var buf bytes.Buffer
acceptsReader(buf)


what was the decision to not have interface parameters be converted as 
well? I don't see how this violates type-safety, since the type is 
guaranteed to implement the interface.

Thank You,
Akhil

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to