Closed
Description
Problem
We can write code like this:
const (
MONDAY int = iota
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY
SATURDAY
SUNDAY
)
But not like this:
const (
MONDAY string = iota
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY
SATURDAY
SUNDAY
)
Proposal
Make iota
return strings with the same value as the const name.
Example, this:
const (
MONDAY string = iota
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY
SATURDAY
SUNDAY
)
Is equal to this:
const (
MONDAY = "MONDAY"
TUESDAY = "TUESDAY"
WEDNESDAY = "WEDNESDAY"
THURSDAY = "THURSDAY"
FRIDAY = "FRIDAY"
SATURDAY = "SATURDAY"
SUNDAY = "SUNDAY"
)
Why?
Simple, it makes writing code that use such enums easier, less boring and less error prone.
One good example is writing code that deals with errors:
const (
ERR_PASSWORD_TOO_SHORT string = iota
ERR_SUPER_USER_PASSWORD_IN_USE
ERR_FAILED_TO_VERIFY_PASSWORD_HIBP
ERR_FAILED_TO_HASH_PASSWORD
)
Compatibility
Current Golang, gives this error when trying to compile code like the one proposed:
cannot convert 1 (type untyped number) to type string
As such I don't think that any old code will become incompatible.
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
[-]proposal: iota for strings (useful for enums)[/-][+]proposal: Go 2: iota for strings (useful for enums)[/+]deanveloper commentedon May 22, 2019
While I like the idea, what would
const Test = iota
be?iota
should always be an untyped integer constant. Perhaps a different name would be more fitting.gjvnq commentedon May 22, 2019
If the type is not specified it becomes a number just like today.
I agree that a sperate name would be better, maybe
siota
(s
meaning string) orstr_iota
orstring iota
(with a space)I think the last option (
string iota
) is better because it does not risk backward compatibility problems. (What if someone has a variable namedsiota
orstr_iota
)mvdan commentedon May 22, 2019
Why declare enum values as strings? Generally, you just need the values to be unique and simple, so integers are fine. If you need to represent them as strings, you can use https://godoc.org/golang.org/x/tools/cmd/stringer.
gjvnq commentedon May 22, 2019
Because printing strings is easier than ints. I don't need a conversion function or a mapping table to print my errors and understand them.
mvdan commentedon May 22, 2019
This is precisely what
String()
methods are for, which is what stringer produces.fmt.Println(someEnumValue)
will work out of the box with stringer, just like any%s
or%v
formatting.deanveloper commentedon May 22, 2019
Making a new predeclared identifier doesn't introduce backward compatibility problems.
iota
is a predeclared identifier (rather than a keyword). Also,iota
is used already in other programming languages to signify a number which increases with each variable declared, I wouldn't useiota
to signify this too, nor would I usestriota
orsiota
or whatever else. I think it should have its own, independent identifier.@mvdan is also right that code generation with the stringer command already takes care of this.
griesemer commentedon May 28, 2019
I would have sometimes liked a feature such like this, but more often than not, I wouldn't want the string to match my constant name exactly. Worse, if one changes a constant's name, for instance when changing whether a constant is exported or not, the string would change, too, which seems not a desirable feature.
It also seems a bit unfortunate to "reuse"
iota
for this purpose:iota
right now has a very simple definition: it's the (untyped integer) index of the constant declaration (specification, to be exact) starting at theconst
keyword. Giving it an extra meaning here adds complexity to the language for a relatively minor convenience.But most importantly, we do have the
stringer
command as pointed out by @mvdan, which specifically addresses this problem. For a typical use with plenty of constants that need string names, see for instance$GOROOT/src/cmd/compile/internal/syntax/tokens.go
. Specifically, the-linecomment
gives you extra flexibility in the choice of string representation.I'm not in favor of this proposal.
MichaelTJones commentedon May 29, 2019
it seems to me that the most natural way to do it would be to allow
which keeps iota unchanged and adds the cast. This compiles now and gives FRIDAY == "\x04", what is new is making the meaning of string in this context be "itoa".
ianlancetaylor commentedon Jun 4, 2019
Thanks, but we already have the stringer tool, and that seems like the right tool for this job.