exec code:
package main
import "fmt"
func main() {
fmt.Println(F() == F())
}
func F() func() int {
i := 0
return func() int {
i++
return i
}
}
i got :
# command-line-arguments
./key.go:6:18: invalid operation: F() == F() (func can only be compared to
nil)
在 2019年1月23日星期三 UTC+8上午11:59:07,[email protected]写道:
>
> When i use the func, map slice as the key of map, it isn't work!
> So I lookup source why, i find
>
> // spec: "The comparison operators == and != must be fully defined
> // for operands of the key type; thus the key type must not be a
> // function, map, or slice."
> //
> // Delay this check because it requires fully setup types;
> // it is safe to continue in any case (was issue 6667).
> check.later(func() {
> if !Comparable(typ.key) {
> check.errorf(e.Key.Pos(), "invalid map key type %s",
> typ.key)
> }
> })
> // Comparable reports whether values of type T are comparable.
> func Comparable(T Type) bool {
> switch t := T.Underlying().(type) {
> case *Basic:
> // assume invalid types to be comparable
> // to avoid follow-up errors
> return t.kind != UntypedNil
> case *Pointer, *Interface, *Chan:
> return true
> case *Struct:
> for _, f := range t.fields {
> if !Comparable(f.typ) {
> return false
> }
> }
> return true
> case *Array:
> return Comparable(t.elem)
> }
> return false
> }
>
--
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 [email protected].
For more options, visit https://groups.google.com/d/optout.