scalatype-members

Values of abstract member types


I'm trying to use this kind of code:

trait Outer {
  type Inner

  def f(x:Inner) : Void 
}

object test {
  def apply(o: Outer, i : Outer#Inner) : Void = 
    o.f(i)
}

I got an error in the last but one line:

type mismatch; found : i.type (with underlying type Outer#Inner) required: o.Inner

If I change apply's signature to

def apply(o: Outer, i : o.Inner) : Void

then I got an error:

illegal dependent method type

Is it possible to get this piece of code work?


Solution

  • You can take advantage of method dependent types (see What are some compelling use cases for dependent method types? by example). This will require you to use 2 parameter lists:

    trait Outer {
      type Inner
      def f(x:Inner): Unit 
    }
    
    object test {
      def apply( o: Outer )( i : o.Inner) { o.f(i) }
    }