Closed
Description
Background: The type checker has two different APIs for the same concept, namely information about a selection of a field or method:
Info.Selections
andMethodSet
both use the record typeSelection
;LookupFieldOrMethod
returns a triple, (obj, indices, indirect).
There is no way to construct a Selection
from the result of LookupFieldOrMethod
. Especially when operating in parallel on the method sets of two types, it is a perennial nuisance that these corresponding items have different forms.
Proposal: We propose a function, Select
, to bridge this gap.
package types
// Select selects the field or method whose ID is Id(pkg, name), on a value of type recv.
// See LookupFieldOrMethod for more detail.
func Select(recv Type, addressable bool, pkg *Package, name string) (Selection, bool) {
obj, index, indirect := LookupFieldOrMethod(recv, addressable, pkg, name string)
var kind SelectionKind
switch obj {
case *types.Func:
kind = MethodVal
case *types.Var:
kind = FieldVal
}
return Selection{kind, recv, obj, index, indirect}, obj != nil
}
Related:
- go/types: API: rename LookupFieldOrMethod to Select (or something select-y) #10091: Closed because it was a rename; this issue proposes an addition.
- go/types: request for NewSelection constructor #12881: superseded by this issue. Also, we have never observed a need for NewSelection other than the one above.
Metadata
Metadata
Assignees
Type
Projects
Status
Accepted