Both of the reflect.Type interface and reflect.Value type implement the same Kind()
method signature, suppose that we have some value object v := reflect.ValueOf(x)
Is v.Kind()
just call v.Type().Kind()
?
Both of the reflect.Type interface and reflect.Value type implement the same Kind()
method signature, suppose that we have some value object v := reflect.ValueOf(x)
Is v.Kind()
just call v.Type().Kind()
?
They contain the same value, but do not seem to refer to the same thing:
A Type
is usually implemented by unexported struct rtype
(via TypeOf
), while the Value
contains a *rtype
and extends flag
, which is itself a reduced form of the Kind
:
// flag holds metadata about the value.
// The lowest bits are flag bits:
// - flagRO: obtained via unexported field, so read-only
// - flagIndir: val holds a pointer to the data
// - flagAddr: v.CanAddr is true (implies flagIndir)
// - flagMethod: v is a method value.
// The next five bits give the Kind of the value.
// This repeats typ.Kind() except for method values.
// The remaining 23+ bits give a method number for method values.
// If flag.kind() != Func, code can assume that flagMethod is unset.
// If typ.size > ptrSize, code can assume that flagIndir is set.
When getting the ValueOf
something:
// ValueOf returns a new Value initialized to the concrete value
// stored in the interface i. ValueOf(nil) returns the zero Value.
func ValueOf(i interface{}) Value {
[...]
// For an interface value with the noAddr bit set,
// the representation is identical to an empty interface.
eface := *(*emptyInterface)(unsafe.Pointer(&i))
typ := eface.typ
/** Flag is built from the type, then kept separate (my comment) */
fl := flag(typ.Kind()) << flagKindShift
if typ.size > ptrSize {
fl |= flagIndir
}
return Value{typ, unsafe.Pointer(eface.word), fl}
}
And so when you get the kind of a Value (remember it extends its flag):
func (v Value) Kind() Kind {
return v.kind()
}
func (f flag) kind() Kind {
return Kind((f >> flagKindShift) & flagKindMask)
}
While getting the kind of a type: (Type
is an interface, usually implemented by *rtype
)
func (t *rtype) Kind() Kind { return Kind(t.kind & kindMask) }
So although they seem to be equal in most of the cases, v.Kind()
is not v.Type().Kind()