IssueRaiser

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

Raise real-world problems, find people facing the same issue, and discuss solutions.3,000 issues0 comments

github (Software)

proposal: spec: generics: type switch on parametric types

rogpeppeApr 4, 2021, 9:18 AM

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.

Comment on this issue