You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
varvsomeStructv=someStruct{x: 1, y: 2, z: 3}
This proposal suggests that the last line could be written as follows:
v= {x: 1, y: 2, z: 3}
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:
FormatFloat(3.14, {}) // use default optionsFormatFloat(2.78, {fmt: ‘e’, bitSize: 32}) // deviate from some default options
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:
This seems related to #21496. Though that is eliding types that are redundant within a single expression, such as nested struct types. Your proposal would go further, and infer context from an entire statement, as far as I understand.
mvdan
changed the title
proposal: allow literal values without preceding literal types
proposal: spec: allow literal values without preceding literal types
Mar 15, 2018
ianlancetaylor
changed the title
proposal: spec: allow literal values without preceding literal types
proposal: Go 2: allow literal values without preceding literal types
Mar 15, 2018
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: