This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Why 'trait_1 extends trait_2' vs 'trait_1 this: trait_2 =>'

4 replies
Barry Kaplan
Joined: 2009-04-01,
User offline. Last seen 42 years 45 weeks ago.

I am struggling for the distinction that helps me decide which to use. It
seems in my current applications that I have been able to use either. What
am I missing?

Joshua.Suereth
Joined: 2008-09-02,
User offline. Last seen 32 weeks 5 days ago.
Re: Why 'trait_1 extends trait_2' vs 'trait_1 this: trait_2 =>
I prefer self types for traits that must be mixed into other classes (but are not themeselves that class).  Basically, for forcing mixin behavior.


scala> class SomeClass(x : Int) {}
defined class SomeClass

scala> trait Mixin { self : SomeClass => }
defined trait Mixin

scala> new Mixin {}
<console>:7: error: illegal inheritance;
 self-type java.lang.Object with Mixin does not conform to Mixin's selftype Mixin with SomeClass
       new Mixin {}
           ^

scala> new SomeClass(5) with Mixin {}
res1: SomeClass with Mixin = $anon$1@134eca



However, There are other handy uses of self types.  Such as making sure you can reference "this" from an outer scope:

trait Outer {
   outer : Outer =>

    trait Inner {
          def foo = outer.doSomethingOuter()
    }
   ....
}


I try to reserve extends for "is-a" relationships and self types for "mixin" behaviors.


Hope that helps!
- Josh

On Wed, May 20, 2009 at 8:09 PM, Barry Kaplan <groups1@memelet.com> wrote:

I am struggling for the distinction that helps me decide which to use. It
seems in my current applications that I have been able to use either. What
am I missing?
--
View this message in context: http://www.nabble.com/Why-%27trait_1-extends-trait_2%27-vs-%27trait_1-this%3A-trait_2-%3D%3E%27-tp23645868p23645868.html
Sent from the Scala - User mailing list archive at Nabble.com.


Barry Kaplan
Joined: 2009-04-01,
User offline. Last seen 42 years 45 weeks ago.
Re: Why 'trait_1 extends trait_2' vs 'trait_1 this: trait_2 =>

So
- self-type when mixin need not be substitutable for mixed trait
- extends when the (sub)trait needs to be substitutable for the super trait

Let me see if that helps clarify things in context. It could just be that
I'm trying to get to fancy too early ;-)

thanks Josh!

Joshua.Suereth
Joined: 2008-09-02,
User offline. Last seen 32 weeks 5 days ago.
Re: Why 'trait_1 extends trait_2' vs 'trait_1 this: trait_2 =>
np.

I actually tried to capture real-life usage "patterns" of traits in this blog.  Not sure if it's helpful, but *I* like the way I think about traits ;)

-Josh

On Wed, May 20, 2009 at 10:03 PM, Barry Kaplan <groups1@memelet.com> wrote:

So
- self-type when mixin need not be substitutable for mixed trait
- extends when the (sub)trait needs to be substitutable for the super trait

Let me see if that helps clarify things in context. It could just be that
I'm trying to get to fancy too early ;-)

thanks Josh!
--
View this message in context: http://www.nabble.com/Why-%27trait_1-extends-trait_2%27-vs-%27trait_1-this%3A-trait_2-%3D%3E%27-tp23645868p23646671.html
Sent from the Scala - User mailing list archive at Nabble.com.


Rob Dickens
Joined: 2008-12-20,
User offline. Last seen 42 years 45 weeks ago.
Re: Why 'trait_1 extends trait_2' vs 'trait_1 this: trait_2 =>
You certainly have to do it the 'B {this: A =>' way if A already extends B.
2009/5/21 Barry Kaplan <groups1@memelet.com>

I am struggling for the distinction that helps me decide which to use. It
seems in my current applications that I have been able to use either. What
am I missing?
--
View this message in context: http://www.nabble.com/Why-%27trait_1-extends-trait_2%27-vs-%27trait_1-this%3A-trait_2-%3D%3E%27-tp23645868p23645868.html
Sent from the Scala - User mailing list archive at Nabble.com.




--
Rob, Lafros.com

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland