3 ms·
Exciting! Especially I like the named attribute matching [1] and dict matching [2]. Scala does not have that. If you have a case (data) class with 13 parameters
by vletal 6y ago
Exciting! Especially I like the named attribute matching [1] and dict matching [2]. Scala does not have that. If you have a case (data) class with 13 parameters, you need to match on all of them, instead of doing `MyClass(param=value)`. Similarly you can not easily pattern match on `Map` (`dict`).
One thing which I am missing though is matching on f-strings. Such a powerful feature in Scala.
[1] https://www.python.org/dev/peps/pep-0636/#adding-a-ui-matching-objects https://www.python.org/dev/peps/pep-0636/#adding-a-ui-matchi...
[2] https://www.python.org/dev/peps/pep-0636/#going-to-the-cloud-mappings https://www.python.org/dev/peps/pep-0636/#going-to-the-cloud...
- 0x008 6y agoI don’t understand what you are saying? In scala you do not have to match on all parameters. You can use the underscore operator to skip the other attributes. Also you can match a map since every map is just a sequence of Tuples in scala. However why would you ever do this? It is really impractical for large sequences and if I have only a very limited number of attributes I would not use a map I guess.
- tomsmeding 6y ago> You can use the underscore operator to skip the other attributes And have to add an underscore in the right position when you ever add an attribute? With partial matching like described, you don't have to change the pattern when adding fields to the data structure. I guess the GP was referring to this.
- 0x008 6y agoWell that is a little bit different I guess. You probably could do some unapply shenanigans to achieve the same result but that off the top of my head I would not know how to do that.
- vletal 6y agoWell sure, wildcards still count as matching. The ability to use named params instead could clean some codebases a bit. Also I get that there are reasons for the Map matching not being present in Scala. All I am saying is that it's cool that it's in python.
- dividedbyzero 6y agoYou can use guards to achieve a lesser version of this in Scala, e.g.: case x:YourCaseClass if(x.oneField=="value") => ...