Accepted answer

There is only one function (A => Foo) for concrete classes with different implementations. I don't see here big advantage of using a type class. I would start considering a type class when one of arguments is generic.

As suggested in comments one could extract pattern matching into fold

def fold[F](a: A)(f1: A1 => F, ..., f4: A4 => F): F = a match {
    case a1: A1 => f1(a1)

and implement required functions:

def aFoo(a: A): Foo = fold(a)(afoo1, afoo2, afoo3, afoo4)
def afoo1(a: A1): Foo = ... 
def afoo4(a: A4): Foo = ...

def bFoo(a: A): Foo = fold(a)(bfoo1, bfoo2, bfoo3, bfoo4)
def bfoo4(a: A4): Foo = ...

But your AFooSupport is already sort of a fold implemented using inheritance instead of composition.


On the first look it seems like you could use generics as a Solution or solve your problem with inheritance.

Related Query

More Query from same tag