Being a case class is just that - it's an inherent quality of the class, not dependent on how you use it. As you can see, MousePressed
is defined as a case class (and it's not a subclass of Java's MouseEvent
BTW, in case that causes confusion), so it's always a case class.
1 and 2 are simply different pattern matching expressions. The difference is that:
- you can use 1 for any type, since you're basically saying "match anything that is an instance of the type
MousePressed
", - for 2 you need something called an extractor - it tells Scala how to decompose the pattern you've provided. Case classes have constructor-derived extractors defined "for free", and that's why you're able to use 2 on
MousePressed
.
In summary - read up the previous chapter on case classes and pattern matching - everything will become clearer.