scala match compared to java switch

There are three differences to keep in mind:


    First, match is an expression in Scala, i.e., it always results in a value.


    Second, Scala’s alternative expressions never “fall through”into the next case.


    Third, if none of the patterns match, an exception named MatchError is thrown.




The result of either case is the unit value‘()’, which is also, therefore, the result of the entire match expression.

你可能感兴趣的:(scala)