I have a code similar to the one below:
def walkTree(list:List[Command]) { list match { case Command1::rest => doSomething(); walkTree(rest) case Command2::rest => doSomethingElse(); walkTree(rest) case Nil => ; } }
I also know that you can map a pattern to a specific type and assign a variable at the same time:
try { ... } catch { case ioExc:IOException => ioExc.printStackTrace() case exc:Exception => throw new RuntimeException("Oh Noes", e); }
Is there a way to combine both types:
def walkTree(list:List[Command]) { list match { case cmd1:Command1::rest => doSomething(); walkTree(rest) case cmd2:Command2::rest => doSomethingElse(); walkTree(rest) case Nil => ; } }
Or do I need to extract each list item before matching?
list scala pattern-matching
pafcio00
source share