*I couldn't find anything previously on the question below, but as ever 
would appreciate any pointers to prior questions/issues etc.*

Consider the following (playground <https://play.golang.org/p/MQuEfeG5c9>):

====
package main

import (
        "fmt"
)

type S struct {
        f func()
}

func (s S) Impl() {}

var _ I = S{}

type I interface {
        Impl()
}

func main() {
        m := make(map[I]bool)
        m[S{}] = true
        fmt.Println(m)
}
====

Per [the spec](https://golang.org/ref/spec#Map_types):

*If the key type is an interface type, these comparison operators must be 
> defined for the dynamic key values; failure will cause a run-time panic.*


this program compiles but then fails with a runtime panic.

However, in this instance, at compile time we know by type-checking alone 
the type of the value used to index m (i.e. S{}) and that that type is not 
comparable... and so can conclude that if this line executes it will panic.

>From a spec perspective, is this a case of keeping the implementation and 
documentation simple, by treating all cases for interface-keyed maps the 
same, i.e. runtime panic?

Or is there some other reason why this case isn't caught by the compiler/go 
vet?

Thanks

-- 
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