-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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: spec: enums as an extension to types #28987
Comments
This comment has been minimized.
This comment has been minimized.
A commonly requested feature for enums is a way to iterate through the valid enum values. That doesn't seem to be supported here. You discuss converting an integer value to I'm not sure but I suspect that this syntax is going to introduce parsing ambiguities. There are many places where a type can appear. You also have to consider cases like
|
Yes, that was my bad. I've updated to include other languages.
Oops... That's my bad. Either way, once there is a syntax for it, it should be easily supported. In your upcoming example I will use
That's true. It's confusing if Under that assumption, the ambiguous code would work as:
which would have the same output as:
|
Non-primitive enums (i.e. struct, array) is an interesting concept! We should also consider
(My up-thumb is a qualified one :-) |
The reason behind picking example:
versus
The symmetry of
I addressed this, I didn't like it because Something that bothers me about the example code is that I don't like the type inference. I think that since we don't typically need to declare enums too often, the extra verbosity makes the code much more readable. For instance:
What is the underlying type of |
Leveraging the const/var (...) pattern, let the first item clarify the type:
Which makes |
Fair point, completely forgot about that. One of those features that don't get used much, haha
I personally think that
is more readable than
Although that's just be a matter of opinion. I think that the extra verbosity helps the readability in this case, and since people shouldn't be declaring enums all the time, it comes at a relatively low cost.
I actually thought about forcing the underlying struct and enum definition to be separate (which this would effectively do). It was actually the initial design, but as I made examples, it raised a few issues. Now, you have defined two types (an internal one for the structure, and the type with the enum to actually be used). So now you have this useless internal type floating around, which is only used to define an enum. Not a huge deal per se, but it's pretty inconvenient and seems like a waste of typing. It would also clutter up autocompleters for those who use them. Another issue is documentation. Presumably you wouldn't want that struct to be exported, because it's only use is to be used by the enum. The issue with not exporting the struct is that now it doesn't appear on godoc.org, so people don't know what fields your enum has. So your two choices are to either export your struct, which is bad design since it's not supposed to be used outside of the enum, or to keep the struct unexported, which makes it invisible to godoc. The Also, defining the enum all at once illustrates that enums are an extension on types, and not types on their own. Doing Also, if you want to define the struct and enum separately, you still can:
Even in the const/var pattern, that doesn't work. B would still be an Either way, this is all just syntax. I'm glad that there haven't been any problems with the actual concept yet! |
I think you are using the wrong associativity: I like the overall concept, but how would you cover "multi-value" enums like this one: https://play.golang.org/p/V7DAZ1HWkN4? From the top of my head one could use
but this would add yet another keyword to the language. How would one extract the bit information? Using Update: I already mixed up the base values. 🙈 |
That's true that it would solve ambiguity with existing data structures since "extensions" on types are usually prefixes (ie slices/arrays, channels, etc). Apply this to structs and it gets a bit more messy in my eyes, especially if we adopt the
versus
I think the second example illustrates that |
But |
You only need to specify the value type if its literal form isn't unique, i.e. numeric types other than int & float64.
Anonymous value types have problems...
Anonymous enum types are valuable (as I described above)...
|
I personally think that bit-flags should not be handled by enums. They are safe to just use as a raw numeric type in my eyes, since with bitflags you can simply ignore any bits which do not have meaning. I do not see a use-case for bitflags to need a construct, what we have for them now is more than enough.
You could equally achieve that with
I will accept that it may be bad for |
Voila! |
I really don't like the |
Maybe you could rev the proposal to incorporate the above insights and alternatives? |
I've added a list of points at the end which summarize what has been talked about so far. |
Thanks! Also you could cover... a) anonymous enums
b) type inference examples for struct and array, essential for anonymous value types: c) the default value; is it the type's zero value or the first enum value? d) |
good |
I haven't directly addressed them, but it is implied they are allowed because
Actually - I'd rather not. Not because I don't like type inference or anything, but these things are mentioned in a bulletted list. The list is meant to be a TL;DR of the discussion, and I don't want to be polluting it with long examples. I personally don't think it's "essential for anonymous value types" or anything, struct and array enums are just as much enums as ints are and really doesn't take much to wrap your head around how they work.
Thanks for the reminder, I'll add that in
I've already used iota in enum definitions within my proposal. |
@networkimprov I've updated the points of discussion @clareyy Please read https://github.com/golang/go/wiki/NoPlusOne |
Re type inference, you have this example, which weakens your case
Re zero value, I often do this: |
Material is not an enum. It is a type just like any other, but the
I'd argue it should. A work-around would be:
|
As a side note: C++11 introduced |
Actually that's very useful, thank you. My C++ knowledge is a bit outdated since my university professor didn't like C++11 haha. I'll separate C and C++ in the list of languages. |
If
|
1, 2, 3, etc are untyped. So you can assign them to any value that has an underlying numeric type. |
I like to go back to the original list of properties of an enum as enumerated (hah!) at the start of this proposal:
I see these as three distinct, in fact orthogonal properties. If we want to make progress on enums in a way that reflects the spirit of Go, we cannot lump all these qualities into a single unified new language feature. In Go we like to provide the elementary, underlying, language features from which more complicated features can be built. For instance, there are no classes, but there are the building blocks to get the effect of classes (data types, methods on any data types, interfaces, etc.). Providing the building blocks simultaneously makes the language simpler and more powerful. Thus, coming from this viewpoint, all the proposals on enums I've seen so far, including this one, mix way too many things together in my mind. Think of the spec entry for this proposal, for instance. It's going to be quite long and complicated, for a relatively minor feature in the grand scheme of things (minor because we've been programming successfully w/o enums for almost 10 years in Go). Compare that to the spec entry for say an slice, which is rather short and simple, yet the slice type adds enormous power to the language. Instead, I suggest that we try to address these (the enum) properties individually. If we had a mechanism in the language for immutable values (a big "if"), and a mechanism to concisely define new values (more on that below), than an "enum" is simply a mechanism to lump together a list of values of a given type such that the compiler can do compile-time validation. Given that immutability is non-trivial (we have a bunch of proposals trying to address that), and given the fact that we could get pretty far even w/o immutability, I suggest ignoring this for a moment. If we have complex enum values, such as If we have simple enum values, such as weekdays that are numbered from 0 to 6, we have A third, and I believe also common enum value is one that is not trivially a constant, but that can be easily computed from an increasing index (such as var (
pascal Lang = newLang(0)
c Lang = newLang(1)
java Lang = newLang(2)
python Lang = newLang(3)
...
) I believe this case is trivially addressed with existing proposal #21473 which proposes to permit var (
pascal Lang = newLang(iota)
c
java
python
...
) which is a very nice and extremely powerful way to define a list of non-constant values which might be used in an enumeration. That is, we basically already have all the machinery we need in Go to declare individual enum values in a straight-forward way. All that is left (and ignoring immutability) is a way to tell the compiler which set of values makes up the actual enum set. An obvious choice would be to have a new type type ProgLang enum{pascal, c, java, python} // maybe we need to mention the element type That is, the enum simply defines the permissible set of values. The syntactic disadvantage of such an approach is that one will have to repeat the values after having declared them. The advantage is the simplicity, readability, and that there are no questions regarding the scoping of enum names (do I need to say I am not suggesting this as the solution to the enum problem, this needs clearly more thought. But I do believe that separating the concerns of an enum into distinct orthogonal concepts will lead to a much more powerful solution. It also allows us to think about how each of the individual properties would be implemented, separately from the other properties. I'm hoping that this comment inspires different ways of looking at enums than what we've seen so far. |
@griesemer this comment contains the core of another enum proposal, any thoughts? The discussion prior to that comment provides more details... |
@networkimprov I deliberately did not start yet another enum proposal because what I wrote above also needs much more thought. I give credit to this (@deanveloper 's) proposal for identifying the three properties of an enum, I think that's a useful observation. What I don't like with this current proposal is that it mixes the declaration of a type with the initialization of values of that type. Sometimes, the type declaration appears to define two types (as with his Also, one thing that I rarely see addressed (or perhaps I missed it) is how an implementation would ensure that a variable of enum type can only hold values of that enum type. If the enum is simply a range of integers, it's easy. But what if we have complex values? (For instance, in an assignment of an enum value to an enum-typed variable, we certainly don't want a compiler have to check each value against a set of approved values.) A way to do this, and I believe @ianlancetaylor hinted at that, is to always map enum values to a consecutive range of integer values (starting at 0), and use that integer value as an index into a fixed size array which contains the actual enum values (and of course, in the trivial case where the index is the enum value, we don't need the array). In other words, referring to an enum value, say Anyway, all these are just some more thoughts on the subject. It's good to keep thinking about this, the more perspectives we have the better. It might well be that at some point we arrive at an idea that feels right for Go. |
@griesemer, else, for, return, break, continue, etc are syntactic sugar for goto ;-) So you don't believe an enum type is worth any effort? We should just use iota-defined values as indices, and let apps panic at runtime on index out-of-bounds instead of failing to compile? If that's not your gist, I think you missed my question: whether the contents of this comment (and discussion preceding it) would dodge your critique of this proposal: #28987 (comment) |
I realize this is just an off the cuff example, but I don't think you can ignore immutability there. If you define a type by a list of mutable values, which can contain pointers, what does it mean for one of those values to then be mutated? Does the type's definition change at runtime? Is a deep copy of the value made at compile type and put in type t struct { n *int }
a := t{new(int)}
var e enum { a }
*a.n += 1
fmt.Println(*e.n) // Is this one or zero?
e = a // Is this legal or not? You could forbid pointers in the base type (for lack of a better term) of an enum declaration, but that makes it equivalent to allowing arrays and structs to be constants when they consist only of types that can be constant #6386 (comment), except that without the immutability you could still easily do confusing things: foo = bar{2}
var e enum{foo} = foo
foo = bar{3}
e = foo // illegal? And, even if you didn't forbid pointers, you'd probably have to forbid types that cannot be compared since you'd need to compare the values to see where and if they map into Mixing types and values like that also allows some strange things like func f(a, b, c int) interface{} {
return enum{a, b, c}
} I'm not sure it could made to work, but, if it could, it would be the kind of thing where the instructions for using it are all footnotes and caveats. I'm all for providing orthogonal primitives, but I don't think what most language's define as enums can be split apart cleanly. Much of them are inherently and purposefully, "let's do a bunch of stuff all at once so that we don't always have to do the same stuff together". |
@jimmyfrasche Fair point - defining an enum as a simple collection of values is probably not good enough if we can't guarantee that the values are immutable. Perhaps immutability needs to be a requirement for values used in enums. Still, I believe immutability is a separate, orthogonal concept. Or put another way, if enums somehow magically could be used to enforce immutability of the enum values, people would use enums to get immutability. I haven't seen such enums yet. And if enum elements are suitably restricted such that immutability is easy (e.g. they must be constants), then we also have enum instructions with "all footnotes and caveats". Regarding the splitting apart of enum features: In older languages, enums simply provided an easy way to declare a sequence of named constants, together with a type. There was little enforcement (if any) that a value of enum type could not contain other (non-enum) values. We have split out these pieces in Go by providing type declarations and @networkimprov I'm not saying we shouldn't have enums, but the demand in actual programs needs to be pretty high for them (for instance, we have |
I would like to use enums for iterating over fairly short lists of things known at compile time, my conception of them having come from Pascal. Unlike @deanveloper's list of reqs, I personally would prefer only the deliberately inflexible:
Thus the compiler knows all the possible values at compile time and may order them.
A compact 'set' would also be nice, functionally almost identical to a map[enum]bool; again, I want to emphasize that I would be totally happy (in fact prefer) that the possible enum values must be known at compile time and are really part of the code, not true data items (n.b. http errors are probably a better example than fruit in practice). If they are unknown at compile time, and are thus data items, then one expects to write more code (to order, check bounds etc.), but this is annoying in very simple cases where the compiler could just deal with it easily.
Can the above be achieved with existing maps? Yes! with (minor) annoyances. However, I think this is a way for the language to express an opinion: "In these very simple cases, that are code really, this is the one true way you should do sequences and sets". (this is very unoriginal and perhaps not directly relevant to @deanveloper 's proposal, sry :/ ) |
I think it would be appropriate to default to implementing behavior like -Wswitch-enum for the new enumeration type - that is, when switching on a given enumeration type, make sure all cases are covered in the absence of a default case, and warn when values are present that are not part of the defined enumeration. This is valuable for software maintenance when adding new cases. |
Go does not have compiler warnings (only errors), however it may be a good thing to add to govet. |
It's |
Any news about this? |
There is no news. There isn't even agreement on what problem to solve. And this is affected by generics in 1.18, with the introduction of interface types with union type elements. |
Would be nice to have, instead of the current hacky way(iota). |
There is no need to change the spec, Go 1.18 has everything that is required to define namespaced and marshalable 'enum types', including both exhaustive and non-exhaustive type switches, where the compiler ensures the assignment of valid values and automatic package main
import (
"fmt"
"qlova.tech/sum"
)
type Status struct {
Success,
Failure sum.Int[Status]
}
var StatusValue = sum.Int[Status]{}.Sum()
func main() {
var status = StatusValue.Success
// exhaustive switch
status.Switch(StatusValue, Status{
status.Case(StatusValue.Success, func() {
fmt.Println("Yay!", status) // output: Yay! Success
}),
status.Case(StatusValue.Failure, func() {
fmt.Println("Nay!", status) // doesn't output
}),
})
} Here's a weekday example to play around with: |
IMO just make it simple as possible: type Status enum[int] {
ALLOWED = iota
BLOCKED
} type Status enum[string] {
ALLOWED = "y"
BLOCKED = "n"
}
func foo(s Status) {
println(s.(string))
}
foo(Status.ALLOWED) |
:)
|
2024 is the year of Go enum (source: personal feeling) |
Did something happen? Your comment makes no sense. |
Every time, when I need to write
|
But it is reachable: |
Exactly! We need enums (or algebraic data types), and exhaustive switch. |
Having been recently working in rust, I've enjoyed two aspects of enums as defined there:
Rust obviously goes a lot further, but I think these two features would be interesting to explore. How about "value" interfaces:
A value interface can contain any number of same-typed constant expressions separated by
Value interfaces would participate in constant conversion so that given a method like:
I could call it:
Value interfaces would re-use the same syntax as non-value interfaces for fallible casting (though on non-interface types):
And if you want the underlying value you can do the opposite:
A switch statement handles value interfaces specially:
(maybe?) The
The zero value for the value interface is the first one in the list.
And that's kind of it? Two things make me think that this may not work so well in Go as they do in Rust:
A final detail to untangle would be if you want your constants to be typed, it'd be nice to support:
I think the compiler could notice this pattern and use the underlying type and value of the constant if it is cast to the interface at definition time. |
Yet another enum proposal
Related: #19814, #28438
First of all, what is the issue with
const
? Why can't we use that instead?Well first of all,
iota
of course only works with anything that works with an untyped integer. Also, the namespace for the constants are at the package level, meaning that if your package provides multiple utilities, there is no distinction between them other than their type, which may not be immediately obvious.For instance if I had my own
mat
(material) package, I'd want to definemat.Metal
,mat.Plastic
, andmat.Wood
. Then maybe classify my materials asmat.Soft
,mat.Neutral
, andmat.Hard
. Currently, all of these would be in the same namespace. What would be good is to have something along the lines ofmat.Material.Metal
,mat.Material.Plastic
,mat.Material.Wood
, and thenmat.Hardness.Soft
,mat.Hardness.Neutral
, andmat.Hardness.Hard
.Another issue with using constants is that they may have a lot of runtime issues. Consider the
following:
Not only is there a lot of boilerplate code where we define the "enum", but there is also a lot of boilerplate whenever we use the "enum", not to mention that it means that we need to do runtime error checking, as there are bitflags that are not valid.
I thought to myself. What even are enums? Let's take a look at some other languages:
C
This ends up being similar to Go's
iota
. But it suffers the same pitfalls that we have withiota
, of course.But since it has a dedicated type, there is some compile-time checking to make sure that you don't mess up too easily.I had assumed there was compile-time checking to make sure that things likeWeekday day = 20
were at least compile-time warnings, but at least withgcc -Wextra -Wall
there are no warnings for it.C++
This section was added in an edit, originally C and C++ were grouped together, but C++11 has added
enum class
andenum struct
which are very similar to Java's (next section). They do have compile-time checking to make sure that you don't compare two different types, or do something likeWeekday day = 20
.Weeday day = static_cast<Weekday>(20)
still works, however. We should not allow something like this. #28987 (comment)Syntax:
Java
An enum is a kind of class. This class has several static members, named after the enum values you define. The type of the enum value is of the class itself, so each enum value is an object.
I personally like this implementation, although I would appreciate if the objects were immutable.
The good thing about this implementation is that you are able to define methods on your enum types, which can be extremely useful. We can do this in Go today, but with Go you need to validate the value at runtime which adds quite a bit of boilerplate and a small efficiency cost. This is not a problem in Java because there are no possible enum values other than the ones you define.
Kotlin
Kotlin, being heavily inspired by Java, has the same implementation. They are even more clearly
objects, as they are called
enum class
instead of simplyenum
.Swift
Proposal #28438 was inspired by these. I personally don't think they're a good fit for Go, but it's a different one, so let's take a look:
The idea becomes more powerful, as you can define "case functions" (syntax is
case SomeCase(args...)
, which allow something likeEnumType.number(5)
being separate fromEnumType.number(6)
. I personally think it is more fitting to just use a function instead, although it does seem like a powerful idea.I barely have any Swift experience though, so I don't know the advantages of a lot of the features that come with Swift's implementation.
JavaScript
This is probably the best you can do in JavaScript without a static type system. I find this to be a good implementation for JavaScript, though. It also allows the values to actually have behavior.
Okay, so enough with other languages. What about Go?
We need to ask ourselves, what would we want out of enums?
iota
really provides)And what is an enum? The way that I have always seen it, enums are an exhaustive list of immutable values for a given type.
Proposal
Enums limit what values a type can hold. So really, enums are just an extension on what a type can do. "Extension" perhaps isn't the right word, but the syntax should hopefully make my point.
The enum syntax should reflect this. The proposed syntax would be
type TypeName <base type> enum { <values> }
The following would be true with enums:
int enum { ... }
would be the type thatHardness
is based on.int enum { ... }
has the underlying typeint
, soHardness
also has the underlying typeint
.var h Hardness = 1
is allowed, butvar h Hardness = 100
is not. This is similar how it is a compile error to dovar u uint = -5
)var h Hardness = int(5)
) of a different type is not allowedvar h Hardness = Hardness(x)
wherex
is an integer variable.go vet
flag. This is becauseh + 1
may not be a validHardness
.Syntax ideas for reading syntax values:
Type.Name
Type
look like a value.Type#Name, Type@Name, etc
Type
is not a value, but it doesn't feel familiar or intuitive.Type().Name
values(Type).Name, enum(Type).Name, etc
values
would be a builtin function that takes a type, and returns its enumeration values as a struct value. Passing a type that has noenum
part would of trivially returnstruct{}{}
. It seems extremely verbose though. It would also clash asvalues
is a pretty common name. Manygo vet
errors may result from this name. A different name such asenum
may be good.I personally believe
values(Type).Name
(or something similar) is the best option, although I can seeType.Name
being used because of it's familiarity.I would like more critique on the enum definitions rather than reading the values, as that is mainly what the proposal mainly focuses on. Reading values from an enum is trivial once you have a syntax, so it doesn't really shouldn't need to be critiqued too much. What needs to be critiqued is what the goal of an
enum
is, how well this solution accomplishes that goal, and if the solution is feasible.Points of discussion
There has been some discussion in the comments about how we can improve the design, mainly the syntax. I'll take the highlights and put them here. If new things come up and I forget to add them, please remind me.
Value list for the enum should use parentheses instead of curly braces, to match
var/const
declaration syntax.Perhaps changing the type syntax from
<underlying type> enum ( values )
toenum <underlying type> ( values )
.int enum ( ... )
->enum int ( ... )
and similar become more readable and consistent with other languages.[]byte enum ( ... )
get resolved to eitherenum []byte ( ... )
or[]enum byte ( ... )
.struct { ... } enum ( ... )
->enum struct { ... } ( ... )
becomes less readable.Add type inference to enum declarations
Use the
Type.Value
syntax for reading enum valuesType.Method
to reference methods, so it wouldn't be quite as bad to reference enum values asType.Value
.Ranging over enum values is not discussed
Type.Slice
which returns a[]Type
Regarding zero values
enum
"attachment" to a type does is limit what values variables of the type can hold. So under this rationale, I think it makes intuitive sense that if the enum for a type doesn't include the zero-value, then declaring a variable with the zero-value should fail to compile. This may seem strange at first, but as long as the compile error message is something intuitive (ieillegal assignment to fooVar: FooType's enum does not contain value <value>
) it shouldn't be much of a problem.The text was updated successfully, but these errors were encountered: