-
Notifications
You must be signed in to change notification settings - Fork 17.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: go/types: add Var.Kind method and enum #70250
Comments
|
|
Ooh, I've wanted this before, and it would make some code of mine simpler. |
@adonovan I guess a better way to phrase my question is do we need both PackageVar and LocalVar? The 1-line snippet you gave suggests we could do without both. (All of the other values LGTM.) |
If you had IsParam and IsResult, then you could deduce IsLocal by exclusion of the other five possibilities. But to implement that you would still need an enum internally, so why not expose it? (We have a set of accessors for the enum that is internal to TypeAndValue. I have often thought it would be more convenient if the mode itself were a first-class enum instead of a set of answers to overlapping boolean predicates. Let's not make that mistake again.) |
What about |
Good point. We should add that too. |
This would be very useful, in general. Bikeshedding, what is the heuristic for the ordering of these enum values? If it is approximate source order, perhaps the following order makes more sense (swapping FieldVar and LocalVar). PackageVar VarKind = iota
FieldVar
RecvVar
ParamVar
ResultVar
LocalVar |
I put FieldVar last because a field is not a variable according to the spec, only according to go/types. But I don't think it really matters. |
This proposal has been added to the active column of the proposals project |
Let's start the enum at 1 so the zero value is invalid. We don't have to give that a name (just |
Based on the discussion above, this proposal seems like a likely accept. The proposal is to add the following to package package types
type VarKind uint8
const (
_ VarKind = iota // The zero value is not meaningful
PackageVar
LocalVar
RecvVar
ParamVar
ResultVar
FieldVar
)
// Kind reports what kind of variable v is.
func (v *Var) Kind() VarKind
// The setter is provided for use by importers.
func (*Var) SetKind(VarKind)
func (VarKind) String() string |
Background: A
types.Var
represents a variable, broadly defined: a global, a local (including parameters and named results), or a struct field. Two of these cases can be discriminated thus:v.IsField()
reports whether the var is a struct field.v.Parent() == v.Pkg().Scope()
reports whether the var is a global.But for the local variables, one is out of luck.
Proposal: We propose to add a Kind method and enum type that reports what kind of var this is.
The actual implementation would replace the existing
isField bool
, so there is no space cost.@griesemer @findleyr @timothy-king
The text was updated successfully, but these errors were encountered: