How is this case class match pattern working?

陌路散爱 提交于 2019-12-17 11:45:49

问题


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 :


回答1:


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.



来源:https://stackoverflow.com/questions/1059145/how-is-this-case-class-match-pattern-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!