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

后端 未结 4 1093
臣服心动
臣服心动 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:56

    One of the key aspects of code readability is the words that grab your attention. For example, return grabs your attention when you see it because you know that it is such a decisive action (breaking out of the function and possible sending a value back to the caller).

    Another example is break--not that I like break, but it gets your attention.

    I would agree with @Mik378 that case in Scala is more readable than the alternatives. Besides the compiler confusion he mentions, it gets your attention.

    I am all for concise code, but there is a line between concise and illegible. I will gladly make the trade of 4n characters (where n is the number of cases) for the substantial readability that I get in return.

提交回复
热议问题