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

I'm my own grandpa

4 replies
extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.

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)
}
}

Joshua.Suereth
Joined: 2008-09-02,
User offline. Last seen 32 weeks 5 days ago.
Re: I'm my own grandpa
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)
 }
}

roland.kuhn
Joined: 2011-02-21,
User offline. Last seen 35 weeks 3 days ago.
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:
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)
 }
}


geoff
Joined: 2008-08-20,
User offline. Last seen 1 year 25 weeks ago.
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.

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: I'm my own grandpa

It's a lot less exciting the way you guys put it.

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