self-type

Java field type for a value of a generically recursive self-type?

Given a class hierarchy where the base class defines a recursive self-type: abstract class A<T extends A<T>> { } How can I declare another class (which should not be generic in T, because such a T could vary over the lifetime of the object) with a field that can hold any subclass of A? The following does not work: public class B { ...

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

Self-types seem to be important so I want to know why they are useful. From what I can gather, a self-type for a trait A: trait B trait A { this: B => } says that "A cannot be mixed into a concrete class that does not also extend B". (sidenote: I'm also seeing this fail in the REPL when mixed into an abstract class that does not e...

Difference between trait inheritance and self type annotation

In Scala, I've seen the constructs trait T extends S and trait T { this: S => used to achieve similar things (namely that the abstract methods in S must be defined before an instance may be created). What's the difference between them? Why would you use one over the other? ...

Self type inheritance in scala

Say I have the following traits: trait A trait B { this: A => } trait C extends B // { this: A => } Compiler error: illegal inheritance; self-type C does not conform to B's selftype B with A As expected if I uncomment the self type annotation, the compiler is happy. I think it's quite obvious why C also needs this self type. What ...