Search code examples
scalainheritancemultiple-inheritancemixins

Parameterise super class with class member of mixin class


trait Mixin {
    class P
}

abstract class SuperClass[T]

class Impl extends SuperClass[P] with Mixin

Gives:

not found: type P

Is there any way to provide the member class P which is mixed in as the parameter to a super class, or is this impossible because of ordering of evaluation (or some other problem?).


Solution

  • This way the scopes don't work out correctly. But type members and type parameters are nearly equivalent, and if you can make T a member of SuperClass instead, you get

    abstract class SuperClass {
      type T
    }
    // optional
    object SuperClass {
      type Aux[T0] = SuperClass { type T = T0 }
    }
    
    class Impl extends SuperClass with Mixin {
      type T = P
    }
    

    In those cases where you want T to be used as a generic parameter, you use SuperClass.Aux[T].