- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Why 'trait_1 extends trait_2' vs 'trait_1 this: trait_2 =>'
Thu, 2009-05-21, 01:09
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?
Thu, 2009-05-21, 03:07
#2
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!
Thu, 2009-05-21, 03:57
#3
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:
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.
Thu, 2009-05-21, 09:17
#4
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>
--
Rob, Lafros.com
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
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: