vipally / go

The Go programming language
https://golang.org
BSD 3-Clause "New" or "Revised" License
0 stars 1 forks source link

reflect: add Type.PtrTo to convert T to *T instead of PtrTo #8

Open vipally opened 6 years ago

vipally commented 6 years ago

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.

vipally commented 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.

1. Compare with follow ways of converting T to ***T, the first way reads/writes clearer.

t.PtrTo().PtrTo().PtrTo()
reflect.PtrTo(reflect.PtrTo(reflect.PtrTo(t)))

2. If somelib.DeepType returns the deep type T for any pointer. Only the first way can get *T without depends on reflect.

somelib.DeepType(p).PtrTo()
reflect.PtrTo(somelib.DeepType(p))
vipally commented 6 years ago

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.