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

an open letter to scalac

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

scalac, I have a burning question for you:

class A {
// Why can't I get my hands on "MyType", if I want to write:
// def otherMe: MyType
// You know you can do this, scalac! I will prove it to you:
def f = this
}
class B extends A { override def f = this }
class C extends B { override def f = this }
class D[T, U, V] extends C { override def f = this }
trait Q { self: C => override def f = this }

object Test {
def x1 = new A f
def x2 = new B f
def x3 = new C f
def x4 = new D[String, Int, List[Int]] f
def x5 = new C with Q { }

// Our mutual buddy scalap says:
//
// def x1 : A = { /* compiled code */ }
// def x2 : B = { /* compiled code */ }
// def x3 : C = { /* compiled code */ }
// def x4 : D[scala.Predef.String, scala.Int, scala.List[scala.Int]]
= { /* compiled code */ }
// def x5 : C with Q = { /* compiled code */ }
//
// and:
//
// class C extends B with scala.ScalaObject {
// override def f : C = { /* compiled code */ }
// }
// class D[T, U, V] extends C with scala.ScalaObject {
// override def f : D[T, U, V] = { /* compiled code */ }
// }
// trait Q extends java.lang.Object with scala.ScalaObject {
// override def f : Q with C = { /* compiled code */ }
// }
}

// See? You know exactly what I'm asking for, scalac.
// You even came up with "C with Q" for god's sake!

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: an open letter to scalac
milessabin
Joined: 2008-08-11,
User offline. Last seen 33 weeks 3 days ago.
Re: an open letter to scalac

On Sat, Dec 17, 2011 at 9:07 PM, Paul Phillips wrote:
> scalac, I have a burning question for you:
>
> class A {
>  // Why can't I get my hands on "MyType", if I want to write:
>  //   def otherMe: MyType
>  // You know you can do this, scalac! I will prove it to you:
>  def f = this
> }

Cheers,

Miles

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: an open letter to scalac

Minor bug:

On Sat, Dec 17, 2011 at 19:07, Paul Phillips wrote:
>  def x5 = new C with Q { }
>
> // See? You know exactly what I'm asking for, scalac.
> // You even came up with "C with Q" for god's sake!

Because f wasn't called in this case.

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: an open letter to scalac

On Sat, Dec 17, 2011 at 6:02 PM, Daniel Sobral wrote:
> Because f wasn't called in this case.

Oops. Well I'm happy to report it's the same when calling f. (If we
can forgive "Q with C" instead of "C with Q".)

object Test {
def x1 = new A f
def x2 = new B f
def x3 = new C f
def x4 = new D[String, Int, List[Int]] f
def x5 = new C with Q { } f
}

object Test extends java.lang.Object with scala.ScalaObject {
def this() = { /* compiled code */ }
def x1 : A = { /* compiled code */ }
def x2 : B = { /* compiled code */ }
def x3 : C = { /* compiled code */ }
def x4 : D[scala.Predef.String, scala.Int, scala.List[scala.Int]] =
{ /* compiled code */ }
def x5 : Q with C = { /* compiled code */ }
}

H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
Re: an open letter to scalac

Am 17.12.2011 23:22, schrieb Miles Sabin:
> On Sat, Dec 17, 2011 at 9:07 PM, Paul Phillips wrote:
>> scalac, I have a burning question for you:
>>
>> class A {
>> // Why can't I get my hands on "MyType", if I want to write:
>> // def otherMe: MyType
>> // You know you can do this, scalac! I will prove it to you:
>> def f = this
>> }
> +ѡ
>
> Cheers,
>
>
> Miles
>
this might be stupid, but i always used "this.type" as a return type.
why doesn't that work for you?

milessabin
Joined: 2008-08-11,
User offline. Last seen 33 weeks 3 days ago.
Re: an open letter to scalac

On Sun, Dec 18, 2011 at 8:12 AM, HamsterofDeath wrote:
> Am 17.12.2011 23:22, schrieb Miles Sabin:
>> On Sat, Dec 17, 2011 at 9:07 PM, Paul Phillips wrote:
>>> scalac, I have a burning question for you:
>>>
>>> class A {
>>>  // Why can't I get my hands on "MyType", if I want to write:
>>>  //   def otherMe: MyType
>>>  // You know you can do this, scalac! I will prove it to you:
>>>  def f = this
>>> }
>
> this might be stupid, but i always used "this.type" as a return type.
> why doesn't that work for you?

this.type is much too precise. In all of the cases where you see the
kind of encoding that Paul exhibited what's wanted is the least
non-singleton type that bounds this.type.

Cheers,

Miles

Rüdiger Klaehn
Joined: 2009-06-02,
User offline. Last seen 42 years 45 weeks ago.
Re: an open letter to scalac
+1000
This is a huge pain in the ass when using traits that return a modified copy of themselves. At the very least there should be a heavy documented standard way to deal with this scenario. But given that you have to do this all the time when combining immutability with inheritance, language support would be really welcome.
On Sat, Dec 17, 2011 at 10:07 PM, Paul Phillips <paulp [at] improving [dot] org> wrote:
scalac, I have a burning question for you:

class A {
 // Why can't I get my hands on "MyType", if I want to write:
 //   def otherMe: MyType
 // You know you can do this, scalac! I will prove it to you:
 def f = this
}
class B extends A { override def f = this }
class C extends B { override def f = this }
class D[T, U, V] extends C { override def f = this }
trait Q { self: C => override def f = this }

object Test {
 def x1 = new A f
 def x2 = new B f
 def x3 = new C f
 def x4 = new D[String, Int, List[Int]] f
 def x5 = new C with Q { }

 // Our mutual buddy scalap says:
 //
 // def x1 : A = { /* compiled code */ }
 // def x2 : B = { /* compiled code */ }
 // def x3 : C = { /* compiled code */ }
 // def x4 : D[scala.Predef.String, scala.Int, scala.List[scala.Int]]
= { /* compiled code */ }
 // def x5 : C with Q = { /* compiled code */ }
 //
 // and:
 //
 // class C extends B with scala.ScalaObject {
 //   override def f : C = { /* compiled code */ }
 // }
 // class D[T, U, V] extends C with scala.ScalaObject {
 //   override def f : D[T, U, V] = { /* compiled code */ }
 // }
 // trait Q extends java.lang.Object with scala.ScalaObject {
 //   override def f : Q with C = { /* compiled code */ }
 // }
}

// See? You know exactly what I'm asking for, scalac.
// You even came up with "C with Q" for god's sake!

H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
Re: an open letter to scalac
i see, it's the "basetrait has a copy method but i am too lazy to update the returntype on each subclass"-case. yes, i know that one. i should read the mails before responding.

+1

Am 18.12.2011 11:16, schrieb Rüdiger Klaehn:
19RuAvG85Ww [at] mail [dot] gmail [dot] com" type="cite">+1000
This is a huge pain in the ass when using traits that return a modified copy of themselves. At the very least there should be a heavy documented standard way to deal with this scenario. But given that you have to do this all the time when combining immutability with inheritance, language support would be really welcome.
On Sat, Dec 17, 2011 at 10:07 PM, Paul Phillips <paulp [at] improving [dot] org" rel="nofollow">paulp [at] improving [dot] org> wrote:
scalac, I have a burning question for you:

class A {
 // Why can't I get my hands on "MyType", if I want to write:
 //   def otherMe: MyType
 // You know you can do this, scalac! I will prove it to you:
 def f = this
}
class B extends A { override def f = this }
class C extends B { override def f = this }
class D[T, U, V] extends C { override def f = this }
trait Q { self: C => override def f = this }

object Test {
 def x1 = new A f
 def x2 = new B f
 def x3 = new C f
 def x4 = new D[String, Int, List[Int]] f
 def x5 = new C with Q { }

 // Our mutual buddy scalap says:
 //
 // def x1 : A = { /* compiled code */ }
 // def x2 : B = { /* compiled code */ }
 // def x3 : C = { /* compiled code */ }
 // def x4 : D[scala.Predef.String, scala.Int, scala.List[scala.Int]]
= { /* compiled code */ }
 // def x5 : C with Q = { /* compiled code */ }
 //
 // and:
 //
 // class C extends B with scala.ScalaObject {
 //   override def f : C = { /* compiled code */ }
 // }
 // class D[T, U, V] extends C with scala.ScalaObject {
 //   override def f : D[T, U, V] = { /* compiled code */ }
 // }
 // trait Q extends java.lang.Object with scala.ScalaObject {
 //   override def f : Q with C = { /* compiled code */ }
 // }
}

// See? You know exactly what I'm asking for, scalac.
// You even came up with "C with Q" for god's sake!


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