I am new to Scala. When I study it while reading Scala code written by others, one of the most distinctive features that I find in Scala code, which differs from other languages, is its comparison with the sample.
At the same time, I feel the convenience and expressiveness that it brings; I cannot but curiously evaluate the potential cost of working behind it - generally speaking, how fast match?
Firstly, without "advanced" functions, such as mapping parameters of constructors, matchin Scala, IMO, is an analogue switch-casein other languages. For instance,
color match {
0 => println "color is red!"
1 => println "color is green!"
2 => println "color is blue!"
}
As a newbie, I want to know if the code above is just as fast as the equivalent code in an expression if-else?
Secondly, now we return these "advanced" functions, for example:
expr match {
Animal(name) => ...
Animal(name, age) => ...
Animal(_, _, id) => ...
}
Regarding the above code or other matching features (matching lists, matching pairs, etc.), I wonder how this bizarre use of Scala is implemented? And most importantly, how quickly can I expect this code to be? (Tell me, are they still as fast as matchin the first case? Or maybe a little slower? Or very slow due to the use of some technologies, such as reflection?)
Thanks in advance!