-
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: permit iota, omission of init expressions in var declarations #21473
Comments
Seems problematic to me: https://play.golang.org/p/c0BE0M_WYI |
@cznic how so? |
I know it's the same as with constant, it's just now double the surface. So questions also like "Why do my var-enums not work?" will be coming. I don't think adding the feature is worth complicating the language. |
@cznic It's arguably less complexity in the language because now the same syntax and mechanism can be used for constants and variables. The question you're afraid might be coming up would have surfaced already now as it's quite common to have sequences of grouped constant declarations. Unrelated: The reason I brought this issue up in the first place is that it might be another stepping stone towards addressing the desire for "enums" by some. Making it easier to declare groups of variables, perhaps together with a mechanism to enumerate over them might be a more Go-like approach than the the more heavy-weight enum support that is found in other languages. |
This would be useful for stuff like |
The main problem I have with this is that these are indeed variables, not constants, with the associated possibility of modification, and the lack of possibility for optimisation. That's a whole other can of worms though, of course. |
@rogpeppe that's valid but the orthogonal concept of creating immutable values could handle that. |
Wouldn't this be unnecessary if we allowed non-{string, numeric} constants? |
@faiface Probably, but that is not a simple change. |
type foo struct {
n int
s string
}
var (
a = foo{n: iota, s: "a"}
b = foo{s: "b"}
) Does |
@jimmyfrasche |
Technically not fully. This code would now fail to compile: https://play.golang.org/p/GIz4UoSAKrD (Today the But I think it's reasonable to assume no one writes obscure code like that except me when I'm coming up with contrived language spec test cases (e.g., #15550). |
This was discussed very early in the language design, at the same time when we also introduced iota for constant declarations: Permit iota and the omission of initialization expressions not only in constant but also variable declarations. For instance, this would permit code like:
If my memory serves me right, we didn't pursue it at the time because there was no compelling or urgent argument in favor of it.
Such a mechanism could simplify the construction of related named objects and open the door to "enum" values that are not restricted to constant values and their respective types.
As such it may be a more elementary building block towards more complex "enums" in Go (see also the discussion in #19814).
Implementation:
Language:
The text was updated successfully, but these errors were encountered: