proposal: spec: Permit range over type sets of rangeable types #71042
Labels
LanguageChange
Suggested changes to the Go language
LanguageChangeReview
Discussed by language change review committee
Proposal
Milestone
Go Programming Experience
Intermediate
Other Languages Experience
No response
Related Idea
Has this idea, or one like it, been proposed before?
No, however there is some overlap in use-cases with #44253
Does this affect error handling?
No
Is this about generics?
Yes, this is lifting a restriction for type sets that contain all rangeable values.
Proposal
Permit using the
range
operation over a parameterized type set where all types can use therange
operation.For example:
Today this code fails to compile with the following message:
Per the language specification, range expressions must have specific core types, and type sets of arrays don't have a core type since every size of array is a different underlying type.
Interestingly, both
len
and the index expression are permitted, so the example code can be rewritten to work using a traditional for loop. This was surprising and it took me reading the language specification to realize that this was another possibility.Language Spec Changes
No response
Informal Change
You can now use a range operation on generic types of mixed arrays and slices.
Is this change backward compatible?
Yes
Orthogonality: How does this change interact or overlap with existing features?
No response
Would this change make Go easier or harder to learn, and why?
Yes, currently most operations are permitted on parameterized types that are type sets of types that allow that operation. eg: the arithmetic operations, index expressions, and the built-in
len
function. This removes what appears to be an exception to that rule.Cost Description
The specification becomes more complicated since the language can no longer lean on the definition of "core type" to describe which parameterized types may use a range operation.
Changes to Go ToolChain
No response
Performance Costs
No response
Prototype
No response
The text was updated successfully, but these errors were encountered: