What's the reasoning behind adding the “case” keyword to Scala?

后端 未结 4 1094
臣服心动
臣服心动 2021-02-13 09:57

Apart from:

case class A

... case which is quite useful?

Why do we need to use case in match? Wouldn

4条回答
  •  无人共我
    2021-02-13 10:46

    Adding to @Mik378's answer:

    When you write this: (a, b) => something, you are defining an anonymous Function2 - a function that takes two parameters.

    When you write this: case (a, b) => something, you are defining an anonymous PartialFunction that takes one parameter and matches it against a pair.

    So you need the case keyword to differentiate between these two.

提交回复
热议问题