Hello, I’m fried fish.

I have written several previous Go generic syntax, case studies, new Go messages. In fact, as some proposals are accepted, new ones are emerging.

No, I find that with generics, people can take things a step further. I saw a “new” proposal “Proposal: Spec: Generics: Type Switch on Parametric Types”, which adds type switch requirements on parameter types after generics.

Learn new Go knowledge along with fish fry!

The new proposals

The new proposal is to add a variant statement that allows generics in switch statements to be more easily constrained by their type parameters.

Such as:

switch type T {
case A1:
case A2, A3:
   ...
}
Copy the code

The T type of switch-type statement can be a generic type parameter, and the case type can be any type, including the constraint type of the generic type.

Suppose the type of type T might be the following:

interface{
    C
    A
}
Copy the code

The approximation elements of generics can be used to constrain:

    interface{
        C
        A1 | A2 | ... | An
    }
Copy the code

There’s even a new way to write case:

case interface {~T}:
Copy the code

With support for generics, there are many possibilities for switch in type and case that require specific feature support, and this proposal comes out of that.

The actual case

Case 1: Multiple type elements

type Stringish interface {
	string | fmt.Stringer
}

func Concat[S Stringish](x []S "S Stringish") string {
    switch type S {
    case string:...casefmt.Stringer: ... }}Copy the code

Type S can support string and FMT.Stringer types, case matching corresponding implementation.

Case 2: Approximate elements

type Constraint interface{~int | ~int8 | ~string
}

func ThisSyntax[T Constraint]( "T Constraint") {
    switch type T {
    case ~int | ~int8:...case ~string:... }}func IsClearerThanThisSyntax[T Constraint]( "T Constraint") {
    switch type T {
    case interface{~int | ~int8 }:
        ...
    case interface{~string} :... }}Copy the code

The type T can have many types. The program uses approximate elements, i.e. the base type is int, int8, string. Any of these types can satisfy this constraint.

For this reason, switch-type should be supported, and case should also support this feature.

A disputed point

As you might imagine, this is a very familiar smell, as if some syntax supports it. Therefore, the most controversial aspect of this proposal is the duplication of the original type assertion.

The original type assertion is as follows:

switch T.(type) {
case string:...default:... }Copy the code

The new type discrimination is as follows:

switch type T {
case A1:
case A2, A3:
   ...
}
Copy the code

At first glance, in fact, type assertion can completely replace the new, that is not repeated construction, building the wheel?

It’s not completely replaced. The differences are as follows:

type ApproxString interface{~string }

func F[T ApproxString](v T "T ApproxString") {
    switch (interface{})(v).(type) {
    case string:
        fmt.Println(v)
    default:
        panic("Brain doesn't go in the fried fish.")}}type MyString string

func main(a) {
    F(MyString("Brain goes into fried fish."))}Copy the code

See the difference? What’s the answer?

The answer is: panic.

You may be wondering, what’s the problem? MyString = MyString; MyString = MyString; MyString = MyString; MyString = MyString

The root cause is that it is of type interface, not string. So came the panic of the Defalut branch.

conclusion

Today, I gave you the latest news about the Go generics, which also has some new news after the last proposal was merged. However, Go officials say they will wait until they are comfortable with generics before moving forward with the proposal.

I believe that there is a high probability that the original switch.(type) and switch Type will be processed in the same logical block at the bottom of Go and then gradually transition.

The purpose of this proposal is to address a number of bugs/requirements introduced with the introduction of generics that require new syntactic structures.

What do you think of this? Feel free to leave a comment below 🙂

If you have any questions, welcome feedback and communication in the comments section. The best relationship is mutual achievement. Your praise is the biggest motivation for the creation of fried fish, thank you for your support.

This article is constantly updated. You can read it on wechat by searching “Brain into fried fish”. GitHub github.com/eddycjy/blo… Already included, learning Go language can see Go learning map and route, welcome Star urged more.