Scala - confusion in a characteristic with its import (inheritance of import)

I have the following code:

trait A { import org.somepackage.C._ } class B extends A { def getValue = value ^^^^^ } object C { var value = 5 } 

The value in class B not displayed, which means that the built-in import of class A not inherited by B , although value clearly visible inside A How to achieve the effect of import inheritance so that I can not explicitly import the same things in several classes where the character A is mixed in?

+6
source share
1 answer

An import that is not a first class entity does not demonstrate the behavior you expect. You can instead rebuild your design to achieve something close:

 trait A with C { } class B extends A { def getValue = value // Now this will work. } trait C { var value = 5 } object C extends C 

This idiom is used in Scalaz 6 for users with minimal imports.

+11
source

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


All Articles