Open vipally opened 6 years ago
A library shouldn't consider only about how to implements, but more about how to use it. It's not too costly to call reflect.PtrTo on every type I guess, if it brings more convenience.
t.PtrTo().PtrTo().PtrTo()
reflect.PtrTo(reflect.PtrTo(reflect.PtrTo(t)))
somelib.DeepType(p).PtrTo()
reflect.PtrTo(somelib.DeepType(p))
var up = reflect.PtrTo // a sign of too much reflecting up(up(up(t)))
It's still not easy to read/write, isn't it?
t.PtrTo().PtrTo().PtrTo()
In fact, reflect.PtrTo is really implements on THE ONLY instance of Type(rtype.ptrTo)
func PtrTo(t Type) Type {
return t.(*rtype).ptrTo()
}
That is to say, export method Type.PtrTo is just export an existing method rtype.ptrTo, there's nothing to do with "implements for every type".
func (t *rtype) PtrTo() Type {
return t.ptrTo()
}
The main point of this issue is, how do we intend to use a Type as.
refer https://github.com/golang/go/issues/23240
reflect.Value has method Addr to convert V to &V, and Elem to convert &V to V but reflect.Type has Elem to convert T to T only, we have to use reflect.PtrTo to achive the other work. To corresponding with Value, Type should have a PtrTo method to convert T to T.