3 ms·
I love how Scala does case match with case classes. You can essentially unpack the attributes of a case class and match on them and put additional guards. Examp
by bko 6y ago
I love how Scala does case match with case classes. You can essentially unpack the attributes of a case class and match on them and put additional guards. Example:
bob match {
case Person("bob", _) => println("hi bob!")
case _ => println("I don't know you")
}
I think its important that match returns something so you can use outside of a function which this doesn't do.
val greeting = bob match { ... }
I thought it would be a fun exercise to write case classes in python, kind of like an improved immutable data class with a match that returns.
https://mleverything.substack.com/p/scalas-case-class-in-python-with-case-matching-97efb58359b https://mleverything.substack.com/p/scalas-case-class-in-pyt...
- _ZeD_ 6y agothe "problem" with scala and case classes is that it's very cumbersone when you have case classes with 30+ fields (and you'll have it, expecially if they came from some SQL table) and you want to match only a sigle field. as they are positional-only - and you also need to specify all the other placeholders, makes reading the match very difficult
- virtualwhys 6y agoIn the proposed case then why not just: case x:Bar if x.foo = ... => ?