proposal: Go 2: allow literal values without preceding literal types #24414
Labels
FrozenDueToAge
LanguageChange
Suggested changes to the Go language
Proposal
v2
An incompatible library change
Milestone
In many cases, it is already clear from the context what type a literal value should be, so it would be convenient to allow for dropping the requirement to explicitly repeat the literal type. For example, in the following code, currently the literal type has to be repeated.
This proposal suggests that the last line could be written as follows:
This in itself is not a major improvement for such simple cases. However, a particular use case I have in mind that this would enable is what in other languages is provided through keyword parameters. Here is an example to illustrate the use case:
In this example, checks against default 0 values are included to test whether a ‘keyword’ parameter was provided at the call site or not. This function can now be used as follows:
Keyword parameters are very useful in other languages (IMHO), and dropping literal types from literal values if the context allows for it would add something very close without adding any extra features to the language.
The text was updated successfully, but these errors were encountered: