Skip to content

go/types: add LookupSelection function #70737

Closed
@adonovan

Description

@adonovan

Background: The type checker has two different APIs for the same concept, namely information about a selection of a field or method:

  • Info.Selections and MethodSet both use the record type Selection;
  • 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:

@findleyr @griesemer @timothy-king

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Accepted

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions