proposal: Address operator for basic and bool literals #39085
Labels
FrozenDueToAge
LanguageChange
Suggested changes to the Go language
Proposal
v2
An incompatible library change
Milestone
Currently in Go, the address operator (
&
) can only be applied to an addressable element (i.e. a variable, pointer indirection, slice indexing, addressable struct fields, and composite literals). However, if you are using numeric, string, or bool literals, the address operator is not supported. This usually forces some awkward workarounds that either use intermediate variables, or boxed values (basic literals wrapped in composite types), or helper functions that return pointers for their passed values.Proposal
As mentioned earlier, the address operator can be applied to composite literals to automatically return the address of their respective values as illustrated below:
This proposal calls for extending this mechanism so that the address operator could also support numeric, string, and boolean literal operands. To minimize impact on the language, the proposal calls for the reuse of the same lexical format used for composite literals, mainly:
The following shows examples of how the address operator would be applied to basic and boolean literal values to automatically return their respective addresses:
By contrast, one of the ways to do the same thing today requires an intermediate set of variable assignments as shown below:
Another workaround uses a sort of type boxing that declares additional types that can wrap basic values inside corresponding struct values:
Some codebase uses yet another workaround that define a set of additional helper functions to provide the convenience of returning the addresses of their passed values as shown:
The text was updated successfully, but these errors were encountered: