复合转换案例:对于具有相同类型关联值的复合枚举案例,我们可以有一个公共值绑定吗? [英] Compound switch cases: may we have a single common value binding for compound enum cases that have the same type of associated value?

查看:85
本文介绍了复合转换案例:对于具有相同类型关联值的复合枚举案例,我们可以有一个公共值绑定吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(当我准备好并几乎完成编写问题时,请重新阅读相应的语言指南部分为我回答了该问题,但问题与解答可能对其他人有用,因此我将其发布

背景

请考虑以下枚举,且个案具有两种不同类型的关联值之一,即 Int String

Consider the following enum, with cases that have one of two different types of associated values, Int or String:

enum Foo {
    case bar(Int)
    case baz(Int)
    case bax(Int)
    case fox(String)
}

switch 语句中执行模式匹配时,我们可能会构造复合案例,每个案例都涵盖了几种可能的匹配模式(输入 case 分支(如果有任何模式匹配):

When performing pattern matching in a switch statement, we may construct compound cases, each covering several possible matching patterns (entering the case branch if any of the patterns match):

func foo(_ foo: Foo) -> Int {
    switch foo {
        case .bar, .baz, .bax: return 42
        case .fox: return 0
    }
}

就像非复合案例一样,复合案例也可能包含值绑定:

Just like non-compound cases, compound cases may also include value binding:

func foo(_ foo: Foo) -> Int {
    switch foo {
        case .bar(let x), .baz(let x), .bax(let x): return x 
        case .fox(let y): return Int(y) ?? 0
    }
}

// or
func foo(_ foo: Foo) -> Int {
    switch foo {
        case let .bar(x), let .baz(x), let .bax(x): return x 
        case let .fox(y): return Int(y) ?? 0
    }
}






问题


  • 是否有可能对复合案例使用单个共同值绑定,其中包括例如,有几个具有相同关联值类型的 enum 例?

  • Is it possible to use a single common value binding for compound cases, which covers the compound of several enum cases that have the same type of associated value?

例如,在上面后面的值绑定示例中,为复合 case

E.g., in the latter value binding examples above, some way to use a single binding functionality for the common-type associated value in the compound case

// not valid
func foo(_ foo: Foo) -> Int {
    switch foo {
        case .bar, .baz, .bax, (let x): return x 
        case .fox: return 0
    }
}


推荐答案

不,这不可能;在上面的值绑定示例中,必须在每个模式中绑定 x ,并且在复合案例中,每个模式都必须单独绑定 x

No, this is not possible; in the value binding examples above, x must be bound in every pattern, and this must hold separately for every pattern in the compound cases.

引用语言指南-控制流 [强调我的]


复合案例还可以包含值绑定。
a复合案例的所有模式必须包含相同的值绑定集,而每个绑定
必须从所有值中获取相同类型的值复合情况下的
模式
。这样可以确保无论复合案例的哪一部分
匹配,案例主体中的代码都可以
始终访问绑定的值
,并且该值始终
具有相同的类型。

Compound cases can also include value bindings. All of the patterns of a compound case have to include the same set of value bindings, and each binding has to get a value of the same type from all of the patterns in the compound case. This ensures that, no matter which part of the compound case matched, the code in the body of the case can always access a value for the bindings and that the value always has the same type.

我尝试在上述复合示例中的一种模式中省略绑定,我们给出了关于该主题的不言自明的错误消息:

I we try to omit the binding in one of the patterns in the compound example above, we are given quite an self-explanatory error message on the subject:

func foo(_ foo: Foo) -> Int {
    switch foo {
        case .bar(_), .baz(let x), .bax(let x): return x 
        case .fox: return 0
    }
}




error: 'x' must be bound in every pattern


即使我们在后面的正文中不使用 x 也是如此

This holds even if we don't use x in the body that follows

func foo(_ foo: Foo) -> Int {
    switch foo {
        case .bar(_), .baz(let x), .bax(let x): return 0 
        case .fox: return 0
    }
} // same error

这篇关于复合转换案例:对于具有相同类型关联值的复合枚举案例,我们可以有一个公共值绑定吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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