加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 怎么样? (管)模式匹配工作?

发布时间:2020-12-16 09:46:39 所属栏目:安全 来源:网络整理
导读:你可以写: str match { case "foo" | "bar" = ... } 乍看起来,它看起来像|可能是一个提取对象,但是: str match { case |("foo","bar") = ... } 不工作。 (我看不出如何可以实现) 那么它是一个魔术的内置操作符? (我相信我之前已经看过这个问题,但是不
你可以写:

str match { case "foo" | "bar" => ... }

乍看起来,它看起来像|可能是一个提取对象,但是:

str match { case |("foo","bar") => ... }

不工作。 (我看不出如何可以实现)

那么它是一个魔术的内置操作符?

(我相信我之前已经看过这个问题,但是不可能搜索…)

解决方法

是的管道(|)是内置的模式匹配(见 the scala language reference)。模式匹配部分(第8节)在第8.1.11节中定义了所谓的模式替代方法。定义说:

A pattern alternative p1 | … | pn
consists of a number of alternative
patterns pi . All alternative patterns
are type checked with the expected
type of the pattern. They may no bind
variables other than wildcards. The
alternative pattern matches a value v
if at least one its alternatives
matches v.

所以是的,管道是内置的,是模式匹配的上下文敏感。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读