Accessing values from path-dependent type mixin
Asked Answered
H

2

6

Is it possible to access values in the outer trait from an inner trait mixin? i.e.:

trait Outer {
  val foo
  trait Inner
}

trait InnerMixin { this: Outer#Inner =>
  def bar {
    // how can I access 'foo' here? smth like Outer.this.foo
  }
}

thanks

Hiers answered 9/10, 2014 at 22:6 Comment(0)
E
5

As you will be able to mix your InnerMixin only inside some extension of outer, maybe you could define it inside an Outer mixin, this way

trait Outer {

  val foo: Int

  trait Inner
}

trait OuterMixin  { this: Outer =>

  trait InnerMixin  { this: Inner =>
    def extension = OuterMixin.this.foo
  }
}

class ActualOuter extends Outer with OuterMixin {
  val foo = 12
  class ActualInner extends Inner with InnerMixin {

  }

}

Note : most of the time, you do not need a self type and you can do just OuterMixin extends Outer and InnerMixin extends Inner.

Elemi answered 9/10, 2014 at 22:37 Comment(0)
R
2

Add a field to Inner that lets you get at it's outer

trait Outer {
  val foo: String

  trait Inner {
    val outer = Outer.this
  }
}

trait InnerMixin { this: Outer#Inner =>
  def bar {
    outer.foo
  }
}
Rapid answered 9/10, 2014 at 22:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.