-
Notifications
You must be signed in to change notification settings - Fork 54
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
Make approach to defining enums consistent across the project #663
Comments
Some research:
As far as The standard approaches I've seen for enums is either: type Foo int
const (
A Foo = iota
B
C
)
func (f Foo) String() string {
switch f {
case A:
return "A"
case B:
return "B"
case C:
return "C"
default:
return "<unknown>"
}
} or type Foo int
const (
A Foo = iota
B
C
)
fooStrings = []string{"A","B","C"} // this can also be map[Foo]string
func (f Foo) String() string {
// lookup string in fooStrings and return it
} |
My proposal:
|
We should definitely have a consistent set of functions to work with types used as enums. Perhaps the decision of type could be decided case-by-case? e.g. in some cases having a string name defined as the enum could be more meaningful, as described in the reference above. But happy to restrict ourselves to For numeric based enums, I think the idea of using a slice or map to define the string values for the enum in a single place is a very good one. Building on Caleb's proposal, how about the following five functions/methods for type
Methods 1 and 2 are 'nicer' to use in internal code with strings, while 3 and 4 are implementing external interfaces which are designed to be very general and so are a little bit less readable. |
I agree with everything - except for naming of (2) Usually methods for returning a type from a string are named Oh, and for |
I'd also add that implementing But if an enum is used in a flag it must implement all three. |
Sure, naming the 2nd method Should we consistently define a none/empty value for each enum? |
#654 introduces the enum type Ecosystem implemented differently to other enums (e.g.
analysis.Mode
andstaticanalysis.Task
andanalysisrun.DynamicPhase
)We should develop a consistent approach to how we define enums:
Some areas that should be covered:
Both:
Numeric based enums:
iota
encoding.TextMarshaler
andencoding.TextUnmarshaler
needed to supportflag.TextVar
and json serializationString based enums:
The text was updated successfully, but these errors were encountered: