Scala pattern matching default -
lets want write following function using scala's pattern matching:
def foo(num: int): int = { num match { case 1 => 0 case x if x%2 == 0 => 1 case _ => _ } }
but of course compilation error line case _ => _
i know can solve changing line like: case x=>x
, why scala's pattern matching doesn't allow me that? it's way me saying don't care what's there return it.
because doesn't make sense. case _
means "match , don't assign value symbol", should => _
mean? if don't care is, mean compiler can put in random value?
case x => x
eagerly match everything , return same value. it's clear, concise, , same amount of characters want. reads can understand happening. literally translate "i don't care what's there, match , return it".
Comments
Post a Comment