proposal: spec: generics: type switch on parametric types
In the discussion for https://github.com/golang/go/issues/45346, [this comment](https://github.com/golang/go/issues/45346#issuecomment-812741029) mentioned the possibility of adding a type switch on parametric types. Given that it's threatening to derail the discussion there, I'm creating this proposal to talk about it. ## Proposal: type switching on type parameters I propose that a new statement be added to the language which allows a function with type parameters to further constrain its type parameters: ``` switch type T { case A1: case A2, A3: ... } ``` The switched type (`T` above) must be the name of a type parameter. The type in a case arm can be any type, including constraint types. The first case is chosen that has a constraint that matches the type parameter. Within a chosen arm of the case statement, the type parameter that's being switched on becomes further restricted by the type selected, just as if it had originally been constrained by that type too. Precisely, given a switched type `T` constrained by `C`, and a type in the switch arm `A`, within the code of the switch arm, `T` takes on the type: ``` interface{ C A } ``` If there are multiple cases mentioned in the arm (`A1`, `A2`, ... `An`), then `T` is constrained by using a union element: ``` interface{ C A1 | A2 | ... | An } ``` ## Example ``` type Stringish interface { string | fmt.Stringer } func Concat[S Stringish](x []S) string { switc
Posted by rogpeppe on Apr 4, 2021, 9:18 AM
Tags
Related issues
- [Meta] Feedback on granular control over Copilot changes
- unexpected status 401 Unauthorized issue
- [MODEL] Measurable quality regression in Claude generation 5 (Fable 5 / Opus 5 / Sonnet 5): worse nonsense detection, ~2x verbosity, under-disclosed model fallb
- [CRITICAL] Subagent spawning and subagent pattern bugs trigger infinite recursion, infinite token usage, grossly inefficient token usage, and lost accumulated s
- Video stops playing after some minutes
- [MODEL] Claude verbose code comments by default — ignores instructions to stop.
Raise real-world problems, find people facing the same issue, and discuss solutions.3,000 issues0 comments
github (Software)
[Meta] Feedback on granular control over Copilot changes
unexpected status 401 Unauthorized issue
[MODEL] Measurable quality regression in Claude generation 5 (Fable 5 / Opus 5 / Sonnet 5): worse nonsense detection, ~2x verbosity, under-disclosed model fallb
[CRITICAL] Subagent spawning and subagent pattern bugs trigger infinite recursion, infinite token usage, grossly inefficient token usage, and lost accumulated s
Video stops playing after some minutes
[MODEL] Claude verbose code comments by default — ignores instructions to stop.
Scope Codex VS Code chats to the current workspace/project
Update on "Co-authored-by: Copilot" in commit messages
proposal: spec: generics: type switch on parametric types
In the discussion for https://github.com/golang/go/issues/45346, [this comment](https://github.com/golang/go/issues/45346#issuecomment-812741029) mentioned the possibility of adding a type switch on parametric types. Given that it's threatening to derail the discussion there, I'm creating this proposal to talk about it. ## Proposal: type switching on type parameters I propose that a new statement be added to the language which allows a function with type parameters to further constrain its type parameters: ``` switch type T { case A1: case A2, A3: ... } ``` The switched type (`T` above) must be the name of a type parameter. The type in a case arm can be any type, including constraint types. The first case is chosen that has a constraint that matches the type parameter. Within a chosen arm of the case statement, the type parameter that's being switched on becomes further restricted by the type selected, just as if it had originally been constrained by that type too. Precisely, given a switched type `T` constrained by `C`, and a type in the switch arm `A`, within the code of the switch arm, `T` takes on the type: ``` interface{ C A } ``` If there are multiple cases mentioned in the arm (`A1`, `A2`, ... `An`), then `T` is constrained by using a union element: ``` interface{ C A1 | A2 | ... | An } ``` ## Example ``` type Stringish interface { string | fmt.Stringer } func Concat[S Stringish](x []S) string { switc
Comments (0)
No comments yet. Start the discussion.