Access values ​​from a path dependent mixin type

Is it possible to access values ​​in an external attribute from an inline mix? 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

+6
source share
2 answers

Since you can only mix your InnerMixin inside some external extension, perhaps you can 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. In most cases, you do not need the self type, and you can only do OuterMixin extends Outer and InnerMixin extends Inner.

+5
source

Add an Inner field that lets you get the look.

 trait Outer { val foo: String trait Inner { val outer = Outer.this } } trait InnerMixin { this: Outer#Inner => def bar { outer.foo } } 
+2
source

Source: https://habr.com/ru/post/976530/


All Articles