I read[1]:
"The current Scala implementation also compiles pattern matching over case
classes into more efficient code than pattern matching using extractors.
One reason for this is that different case classes are known not to
overlap, i.e. given two patterns C(...) and D(...) where C and D are
different case classes, we know that at most one of the patterns can
match. The same cannot be assured for different extractors."
I understand it is impossible to do `C with D` for any class because in
Scala's multiple inheritance linearization, only one may be a (concrete or
abstract) class and the rest on the inheritance tree must be traits.
So it seems the above optimization could be done for extractors that input
a class (not just a case class) and return an Option of the same class,
and where it is known for example that C and D don't extend one or the
other? Correct? Does the pattern matcher do this now?
Additionally traits can be joined into a conjunction employing `with`, but
what if there was some keyword that could be prefixed to a base
(supertype) trait (e.g. supertype of C and D when C and D are traits) to
indicate that subtypes can never be joined into the same conjunction, then
the above said optimization of pattern matching could also apply?
Additionally, thus it seems not only do we need a first-class disjunction
for the significant use-case I noted today:
https://groups.google.com/d/msg/scala-debate/LWBz3-Q0pNI/MzuU-5HgFyQJ
Then we also need to be able to declare a disjunction that can only
reference instances that are not conjunctions of the types of the
disjunction (so we can most efficiently downcast them with match-case
pattern matching).
P.S. I am at the forehead on the keyboard, drooping eyelids held open with
toothpicks, so if the above doesn't make sense, please tell me.
[1] Case Classes and Extractors, pg 15. of Matching Objects With Patterns,
Emir, Odersky, Williams