proposal: spec: avoid repeating concrete type parameters when generic arguments are passed to generic functions #53138
Labels
FrozenDueToAge
generics
Issue is related to generics
Proposal
TypeInference
Issue is related to generic type inference
Milestone
Author background
Would you consider yourself a novice, intermediate, or experienced Go programmer?
Experienced.
What other languages do you have experience with?
Java; Scala; C/C++; Python
Related proposals
Has this idea, or one like it, been proposed before?
I don't believe so.
Does this affect error handling?
No
Is this about generics?
Yes
This is a proposal for an improvement to the inference procedure to reduce redundancy.
Proposal
Today, whenever a generic argument is passed to a generic function, you must specify the concrete type at the call-site and repeat this type for each argument.
This proposal is to modify the type inference procedure to allow the types specified at the callsite to be inferred for arguments.
The utility of this change is more easily seen when passing generic functions to other generic functions.
This should improve readability by allowing users to make calls to generic libraries more tersely than they would need to today. It would also enhance the utility of generic libraries which seek to offer functional options to their users for configuration.
Authors and users generic libraries, who want to be able to pass generic type parameters to generic functions without repeating themselves unnecessarily.
The main driver for this proposal is this function call, which is rather verbose (snippet below).
Whenever a generic argument
X[T]
is seen in a call to a generic functionfunc Foo[T](X[T])
, any concrete types seen in the parameter for calls involvingFoo
are applied during type inference for the arguments. This allows usage likeFoo[string](X)
to compile, whereas today, usage likeFoo[string](X[string])
is required.See the proposed change to the language spec below.
At first blush, adding a step to the current type inference procedure, before step 1 seems like it would avoid the duplication involved.
Other portions of the spec may need to be modified as well to take this change into account.
Please also describe the change informally, as in a class teaching Go.
Is this change backward compatible?
Yes; since this change would just make syntax which is required today unnecessary.
Show example code before and after the change.
Before
See playground link containing the below code.
Orthogonality: how does this change interact or overlap with existing features?
This change has the potential to interact with downstream stages of the type inference procedure, and this should be considered carefully.
Is the goal of this change a performance improvement?
No.
Costs
Would this change make Go easier or harder to learn, and why?
Unclear. On the one hand, one could argue that this may may Go somewhat easier to learn because having to specify the types in every case is a bit confusing; especially when there can only be one answer. Another argument may be that having to place explicit types everywhere is clearer, even when redundant.
What is the cost of this proposal? (Every language change has a cost).
Time taken to implement and potentially unforeseen consequences of modifying type inference.
How many tools (such as vet, gopls, gofmt, goimports, etc.) would be affected?
A non-zero number of them. I am unsure.
What is the compile time cost?
Type inference could become slightly more expensive.
What is the run time cost?
None.
Can you describe a possible implementation?
Before beginning type inference for a generic argument to a generic function argument; add any concrete types from the parameter list of the generic function to the parameter map. Track whenever this is done so that the compiler can also ensure that code is generated for these arguments.
No.
The text was updated successfully, but these errors were encountered: