Pattern matching with conjunctions (PatternA AND PatternB)

前端 未结 2 1733
难免孤独
难免孤独 2020-12-07 07:25

Scala has a language feature to support disjunctions in pattern matching (\'Pattern Alternatives\'):

x match {
    case _: String | _: Int => 
    case _          


        
2条回答
  •  孤街浪徒
    2020-12-07 08:23

    A possible problem with this is the bloated translation that the pattern matcher generates. Here is the translation of the sample program, generated with scalac -print. Even -optimise fails to simplify the if (true) "_" else throw new MatchError() expressions.

    Large pattern matches already generate more bytecode than is legal for a single method, and use of this combinator may amplify that problem.

    If && was built into the language, perhaps the translation could be smarter. Alternatively, small improvements to -optimise could help.

提交回复
热议问题