- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
I'm my own grandpa
Fri, 2011-07-29, 19:46
So what do you think, should we do something about this?
% scala Test
T3: 9 ..... passing to super!
T2: 8 ..... passing to super!
T1: 7 ..... passing to self-super!
T3: 6 ..... passing to super!
T2: 5 ..... passing to super!
T1: 4 ..... passing to self-super!
T3: 3 ..... passing to super!
T2: 2 ..... passing to super!
T1: 1 ..... passing to self-super!
T3: 0 ..... and that's all she wrote.
// The program in question
object Test {
abstract class Base {
def f(x: Int): Unit = ()
def report(who: String, target: String, x: Int) {
println(
who + ": " + x + " ..... " + ( if (x > 0) "passing to " + target + "!" else "and that's all she wrote." )
)
}
}
trait T1 {
self: Base =>
override def f(x: Int) {
report("T1", "self-super", x)
if (x > 0)
self.f(x - 1)
}
}
trait T2 extends T1 {
self: Base =>
override def f(x: Int) {
report("T2", "super", x)
if (x > 0)
super.f(x - 1)
}
}
trait T3 extends T2 {
self: Base =>
override def f(x: Int) {
report("T3", "super", x)
if (x > 0)
super.f(x - 1)
}
}
class Bippy extends Base with T3 with T2 with T1 { }
def main(args: Array[String]): Unit = {
val b = new Bippy
b.f(9)
}
}
Fri, 2011-07-29, 20:57
#2
Re: I'm my own grandpa
Maybe I know too little, but I thought that “self” would in this case be the same as “this”, where incidentally also its type is changed to effectively “Base with T1”. Which seems perfectly consistent with the observed behavior. The only problem is that I was unable to figure out a way of invoking the self-type’s version of a method using static dispatch. Is that what you want fixed, Paul?
On Jul 29, 2011, at 21:46 , Josh Suereth wrote:
On Jul 29, 2011, at 21:46 , Josh Suereth wrote:
Seems to me like the answer should be 'YES'. after calling T1 it should return () from Base...
At least if I recall the rules on traits and overriding from the spec correctly.
On Fri, Jul 29, 2011 at 2:46 PM, Paul Phillips <paulp@improving.org> wrote:So what do you think, should we do something about this?
% scala Test
T3: 9 ..... passing to super!
T2: 8 ..... passing to super!
T1: 7 ..... passing to self-super!
T3: 6 ..... passing to super!
T2: 5 ..... passing to super!
T1: 4 ..... passing to self-super!
T3: 3 ..... passing to super!
T2: 2 ..... passing to super!
T1: 1 ..... passing to self-super!
T3: 0 ..... and that's all she wrote.
// The program in question
object Test {
abstract class Base {
def f(x: Int): Unit = ()
def report(who: String, target: String, x: Int) {
println(
who + ": " + x + " ..... " + ( if (x > 0) "passing to " + target + "!" else "and that's all she wrote." )
)
}
}
trait T1 {
self: Base =>
override def f(x: Int) {
report("T1", "self-super", x)
if (x > 0)
self.f(x - 1)
}
}
trait T2 extends T1 {
self: Base =>
override def f(x: Int) {
report("T2", "super", x)
if (x > 0)
super.f(x - 1)
}
}
trait T3 extends T2 {
self: Base =>
override def f(x: Int) {
report("T3", "super", x)
if (x > 0)
super.f(x - 1)
}
}
class Bippy extends Base with T3 with T2 with T1 { }
def main(args: Array[String]): Unit = {
val b = new Bippy
b.f(9)
}
}
Fri, 2011-07-29, 21:17
#3
Re: I'm my own grandpa
On Fri, Jul 29, 2011 at 09:52:38PM +0200, Roland Kuhn said
> Maybe I know too little, but I thought that “self” would in this case
> be the same as “this”, where incidentally also its type is changed to
> effectively “Base with T1”. Which seems perfectly consistent with the
> observed behavior. The only problem is that I was unable to figure out
> a way of invoking the self-type’s version of a method using static
> dispatch. Is that what you want fixed, Paul?
That's what my reading of the spec leads me to as well.
If a formal parameter is given, it can be used as an alias for the
reference this throughout the body of the template.
If you replace self with this in the program then you (unsurprisingly)
get the same result.
Fri, 2011-07-29, 23:07
#4
Re: I'm my own grandpa
It's a lot less exciting the way you guys put it.
At least if I recall the rules on traits and overriding from the spec correctly.
On Fri, Jul 29, 2011 at 2:46 PM, Paul Phillips <paulp@improving.org> wrote: