Question

Possible Duplicate:
What is the difference between scala self-types and trait subclasses?

I can't get the the difference between the two following code blocks:

    // Trait B is mixed in and creates a dependency on it
    trait A extends C with B { 
       ...
    }

    // Trait B is put in scope and also creates a dependency on it
    trait A extends C {
       self: B =>
    ...
    }

I'm asking from a design perspective.

Thanks!

Was it helpful?

Solution

When you use the self-type you constrain the trait to be used only when the specified self-type is satisfied by the other types with which it is mixed. You don't get an inheritance relationship between the trait being defined and the declared self-type. An implication of this is that the trait itself, as a static type in isolation, is not on its own publicly substitutable for the self-type. (It has been likened to C++ private inheritance, but it's a weak analogy).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top