I'm wondering why fallthrough isn't allowed in a type switch statement in golang.
According to the specification: "The "fallthrough" statement is not permitted in a type switch.", which doesn't explain much about WHY it isn't allowed.
The code attached is to simulate a possible scenario were a fallthrough in a type switch statement might have been useful.
Notice! This code doesn't work, it will produce the error: "cannot fallthrough in type switch". I'm just wondering what possible reasons might have been for not allowing the fallthrough statement in a type switch.
//A type switch question package main import "fmt" //Why isn't fallthrough in type switch allowed? func main() { //Empty interface var x interface{} x = //A int, float64, bool or string value switch i := x.(type) { case int: fmt.Println(i + 1) case float64: fmt.Println(i + 2.0) case bool: fallthrough case string: fmt.Printf("%v", i) default: fmt.Println("Unknown type. Sorry!") } }
Code Inspection: Fallthrough in 'switch' statementReports a switch statement where control can proceed from a branch to the next one. Such "fall-through" often indicates an error, for example, a missing break or return .
Fall through condition: This condition occurs in the switch control statement when there is no break keyword mention for the particular case in the switch statement and cause execution of the cases till no break statement occurs or exit from the switch statement.
Fall through is a type of error that occurs in various programming languages like C, C++, Java, Dart …etc. It occurs in switch-case statements where when we forget to add break statement and in that case flow of control jumps to the next line. until the break is reached or end of the switch is reached.”
If you want your switch statement fall through or you want C style fallthrough feature then you can use fallthrough statement. This statement is used to forcefully execute the case present next after the matched statement even though the case is not matching the specified condition.
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 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With