为什么类型开关中不允许fallthrough? [英] Why isn't fallthrough allowed in a type switch?

查看:35
本文介绍了为什么类型开关中不允许fallthrough?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么 golang 的类型 switch 语句中不允许 fallthrough.

I'm wondering why fallthrough isn't allowed in a type switch statement in golang.

根据规范:类型切换中不允许使用fallthrough"语句.",这不详细解释为什么不允许这样做.

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.

附加的代码是为了模拟一个可能的场景,如果类型 switch 语句中的失败可能有用.

The code attached is to simulate a possible scenario were a fallthrough in a type switch statement might have been useful.

注意!这段代码不起作用,它会产生错误:cannot fallthrough in type switch".我只是想知道不允许在类型切换中使用 fallthrough 语句的可能原因是什么.

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!")
    }
}

推荐答案

您希望 fallthrough 怎样工作?在此类型开关中,i 变量的类型取决于调用的特定情况.因此,在 case bool 中,i 变量的类型为 bool.但在 case string 中,它被输入为 string.所以要么你要求 i 神奇地改变它的类型,这是不可能的,或者你要求它被一个新的变量 i string 遮蔽,它没有值,因为它的值来自 x,实际上它不是 string.

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?
}

唯一可能的解决方案是让 fallthrough 导致随后的 case 表达式将 i 作为 interface{},但是将是一个令人困惑和糟糕的定义.

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
}

这篇关于为什么类型开关中不允许fallthrough?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆