Let's say I have
sealed trait AlphaNumericChar
sealed trait AlphaChar
case object A extends AlphaNumericChar with AlphaChar
case object B extends AlphaNumericChar with AlphaChar
sealed trait NumericChar
case object One extends AlphaNumericChar with NumericChar
case object Two extends AlphaNumericChar with NumericChar
This structure allows me to pattern match on AlphaNumericChar
and get all A,B,One,Two
and similarly pattern match on AlphaChar
and NumericChar
and get only the relevant objects.
What it doesn't allow me, is to write:
def foo(x: AlphaNumericChar) = ???
def bar(x: AlphaChar) = foo(x)
ie, proxy calls to foo
for only certain types. I can of course write instead:
def baz(x: AlphaNumericChar with AlphaChar) = foo(x)
and that would work but that's perhaps a bit ugly.
Alternative is to make AlphaChar
and NumericChar
extend AlphaNumericChar
but that would mess with my pattern matches on AlphaNumericChar
as I will now have to handle _:AlphaChar
and _:NumericChar
in addition to my case objects which is undesirable.
Is there a way of somehow having the best of two worlds? ie.
- The exhaustive list of pattern match entries on
AlphaChar
/NumericChar
has only two elements. - The exhaustive list of pattern match entries on
AlphaNumericChar
has only two elements. - I can have the
bar
function above working without resorting to thebaz
syntax.