这个案例类匹配模式是如何工作的? [英] How is this case class match pattern working?

查看:44
本文介绍了这个案例类匹配模式是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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

为什么不是:

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

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

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

推荐答案

在进行模式匹配时,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.

这篇关于这个案例类匹配模式是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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