I was just watching a video by rockthejvm that can be found here. My only issue is that it fails to address if match types can do anything method overloading cannot? Is there anything that match types do better than method overloading? It seems they are just a less extensible version of method overloading.
Any wisdom would be greatly appreciated.
This example from the documentation:
can't be translated to overloading. The non-recursive cases are easy:
but what would you do with arrays? If you write
then:
T
must be the parameter type of aleafElem
overload" required for the body to compile.Or consider this non-recursive example
With match types you can write
If you had overloaded
elem
methods for separate cases,mapElems
wouldn't compile (and again you couldn't write its return type if it did).