Why isn’t fallthrough allowed in a type switch?

How would you expect fallthrough to work? In this type switch, the i variable has a type that depends on the particular case that’s invoked. So in the case bool the i variable is typed as bool. But in case string it’s typed as string. So either you’re asking for i to magically morph its type, which isn’t possible, or you’re asking for it to be shadowed by a new variable i string, which will have no value because its value comes from x which is not, in fact, a string.


Here’s an example to try and illustrate the problem:

switch i := x.(type) {
case int:
    // i is an int
    fmt.Printf("%T\n", i); // prints "int"
case bool:
    // i is a bool
    fmt.Printf("%T\n", i); // prints "bool"
    fallthrough
case string:
    fmt.Printf("%T\n", i);
    // What does that type? It should type "string", but if
    // the type was bool and we hit the fallthrough, what would it do then?
}

The only possible solution would be to make the fallthrough cause the subsequent case expression to leave i as an interface{}, but that would be a confusing and bad definition.

If you really need this behavior you can already accomplish this with the existing functionality:

switch i := x.(type) {
case bool, string:
    if b, ok := i.(bool); ok {
        // b is a bool
    }
    // i is an interface{} that contains either a bool or a string
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)