Pattern Match “return” value

后端 未结 2 1440
孤街浪徒
孤街浪徒 2021-01-18 00:58

Why is it not possible to chain pattern matching constructs? For instance, the following is legal, if nonsensical,

val a = ADT(5)

val b = a match {
  case          


        
2条回答
  •  盖世英雄少女心
    2021-01-18 01:20

    Your intuition is correct; it's not nonsense—normally you would be able to chain infix operators in such a way, without parentheses (as other users have suggested). Indeed, match used to be implemented as a method—and worked as an infix operator (left-associative by default)—so your alternate syntax would have worked. However, in Scala 2.5 match was made a special language construct instead of a method. I don't know why that was done, but that's the reason: match is not an infix operator despite seeming so.

提交回复
热议问题