这个案例类匹配模式是如何工作的?

发布于 2024-07-26 12:21:58 字数 392 浏览 3 评论 0原文

我刚刚在 Scala Actors 包中看到了这个案例类:

case class ! [a](ch: Channel[a], msg: a)

并且在 JavaDoc 中,它以以下形式描述了用法:

receive {
  case Chan1 ! msg1 => ...
  case Chan2 ! msg2 => ...
}

为什么不是:

receive {
  case !(Chan1, msg1) => ...
  case !(Chan2, msg2) => ...
}

是 bang 运算符! 特殊情况与以冒号结尾的方法类似:

I've just seen this case class in the Scala actors package:

case class ! [a](ch: Channel[a], msg: a)

And in the JavaDoc it describes usage in the following form:

receive {
  case Chan1 ! msg1 => ...
  case Chan2 ! msg2 => ...
}

Why is this not:

receive {
  case !(Chan1, msg1) => ...
  case !(Chan2, msg2) => ...
}

Is the bang operator ! a special case in a similar way to methods ending in a colon :

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

咿呀咿呀哟 2024-08-02 12:21:58

进行模式匹配时,Scala 编译器会将 o1 c1 o2 解释为 c1(o1, o2)。 这就是为什么 :: 也可以在模式匹配中工作。

When doing pattern matching, the Scala compiler will interpret o1 c1 o2 the same as c1(o1, o2). That's why :: works inside pattern matches too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文