On Mar 3, 2024, at 12:25 AM, Nicolas Serna <serna.nic...@gmail.com> wrote:Hello, gophers. Lately I've been working quite a bit with enums as I'm moving a C program I had tGo, so I wondered if there was any progress on enums proposals and realized that none of them get anywhere without breaking the Go1 compatibility.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/134fd31b-9081-4d22-b098-412244338fc5n%40googlegroups.com.
What I find valuable is to be able to accept an enum as a parameter to a function, and know that it will be one of several approved values (e.g. month of the year)
On Mar 4, 2024, at 12:18 PM, Jeremy French <ibi...@gmail.com> wrote:More, to prevent PrintMonth(14) which the function would have to check for and either return an error or panic, since there is no meaningful output. ... I was more just answering Mike Schinkel's question about why it would be useful.
On Monday, March 4, 2024 at 10:47:27 AM UTC-5 Brian Candler wrote:Does this represent the problem statement?
1. In order to get immutability without significant boilerplate, we need to use `const`.2. We *can* define a type like `Month` as Brian Candler's example showed.3. We also can assign values of our type to our `const` constants.4. However, we can *only* assign literals or simple expressions to a `const`. i.e. we cannot assign `Month{1}` to a `const` if `Month` were a `struct`, `map`, `array`, or `slice`.5. Further, funcs allow literals to be used as a type without requiring them to be cast as that type which is why `PrintMonth(14)` is both possible and problematic.6. All values that we can validly assigned to a `const` are literal values that can also stand-in for the type when passed to a func that expects that type as a parameter.
1. Allow a `const` to somehow be defined as `strict` where `strict` is a placeholder for the concept; i.e. that `PrintMonth(14)` would not be possible but `PrintMonth(April)` would be.2. Allow a `func` too somehow have its parameter defined as `strict`, i.e. that again, `PrintMonth(14)` would not be possible but `PrintMonth(April)` would be.3. Allow a `var` to somehow be defined as immutable thus allowing immutable vars andtypes of subtypes `struct`, `map`, array, and/or slices to be used as pseudo-enums.
4. Relax the allowable values that can be assigned to a `const` to include values that can be fully determined at compile time such as `const April = [1]byte{4}` thus allowing `const` andtypes of subtypes `struct`, `map`, array, and/or slices to be used as pseudo-enums.
On Mar 4, 2024, at 5:14 PM, Ian Lance Taylor <ia...@golang.org> wrote:P.S. Seems to me that #4 might be the simplest way forward as it would allow for creation of types that cannot be represented as their literal subtype value to pass as a parameter to a `func`, and it would not require any new keywords or concepts added to Go the language, or at least I don't think it would?
Is there any reason Go could not relax the allowable values for const to be able to support constructs that can be determined at runtime such as `const April = [1]byte{4}`?
I think this is https://go.dev/issue/6386.
1. It is unclear where to draw the line on what to allow because of all the ramifications, and2. "Nice to have" is not sufficient reason to add a feature to the language.
The simple version only works if the variable
can be declared and initialized in the same statement. That isn't
alway feasible, which is why init functions exist. It absolutely
prohibits circular data structures, which could be limiting.
The only real downside I can see is if implementing this would someone paint the language in a corner so that it could not be enhanced in a better way in the future. Maybe my vision is too limited, but that doesn't seem likely with such a small, backward compatible change that introduces nothing new other than relaxing an existing constraint.1. The subset to include would only be those things that would be needed to address the enum use-case, which I think would be fixed-sized arrays and structs (and not maps nor slices I previously pondered.)Those should be able to contain only other constants, which I think would include contained fixed-sized arrays and structs, but nothing with a pointer and attempting to take the address of any of those constants should throw a compiler error.2. This proposal would not be because it is "nice to have" but instead it would enable a developer to create function whose parameters could only be satisfied by one of the values of a type used for enums as specified by the developer. IOW, it solves a legitimate development concern and is not just a nice-to-have.