proposal: spec: Values assigned to multi-case branches in type switches should have generics-style #69705
Open
2 of 4 tasks
Labels
generics
Issue is related to generics
LanguageChange
Suggested changes to the Go language
LanguageChangeReview
Discussed by language change review committee
Proposal
Proposal-Hold
Milestone
Go Programming Experience
Experienced
Other Languages Experience
C, C++, Python, Rust, Haskell
Related Idea
Has this idea, or one like it, been proposed before?
#65031 seems similar, although the details differ.
#57644 is possibly related, but I can't tell from its description what effect (if any) it would have on type switches.
Does this affect error handling?
No
Is this about generics?
Yes -- this proposal is to make multi-case type switches match the semantics of generic functions with a set of permitted types.
Proposal
In Go 1.23, a value assigned as the match result in a multi-case type switch retains the original type being inspected. This prevents it from being passed to a function with a type parameter constraint that's only satisfied by the narrowed type.
I propose to change the semantics of multi-case type switches so that the type of the matched variable becomes the intersection of the matched types, plus the original type:
The matched case must have types that are a subset of the generic type:
The type of
v
should also work with the semantics of a generic type parameter for regular code within the case, for example performing conversions that are valid for all matchable types:Language Spec Changes
No response
Informal Change
No response
Is this change backward compatible?
I think so? Given existing and proposed behavior, I believe that any existing code would continue to compile and run without changes.
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?
No response
Cost Description
No response
Changes to Go ToolChain
No response
Performance Costs
No response
Prototype
No response
The text was updated successfully, but these errors were encountered: