No by-name parameter type allowed here

I’m not sure whether extractor objects is possible. The way the dispatch works (i.e., the way rteIfThenElse works is that needs access to all the patterns to do its computation. Could extractor objects have access to each other? Or do they need to work independently? At runtime, the sequence should be traversed at most once.

That being said, it would be nice if I as an application programmer, could provide an API which allows the user to write something that looks like a case match.

In scala pattern matching, who decides how dispatch occurs? I.e., is it the compiler that contains the algorithm for expanding the case match into a flow diagram, or am I, the programmer, allowed to implement a match algorithm for my kind of objects? If that is the case I could potentially provide an interface which allows the following. Can I write a match method on MySpecialWrapperObject ?

val f: Seq[Any] => Int = seq => MySpecialWrapperObject(seq) match {
  case Star(int) => {
    println("case 1")
  case Star(str) => {
    println("case 2")
  case _ => /*default case*/

Usually not, and I can’t think of a way to pass them to each that does not require unchecked duplication of the cases (e.g having them in a list and then also writing the same in the case lines).

You can’t customize how the match keyword works based on the matched object, the patterns are always checked in the order given and the first that matches is executed. I think with the requirement that the sequence should only be traversed once, pattern matching sadly is not suitable for your usecase.


I was under the impression that a pattern match compiled as an efficient decision diagram to eliminate duplicate checking, including smart inclusion checks. I.e., if we have checked and discovered that x is an element of type A, then we know that it is also an element of B when B is a supertype of A, thus the second type check can be optimized away.