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

Why is self type reference not equivalent to extends with regards to type compatibility (or is this a compiler bug?)

6 replies
Maxime Lévesque
Joined: 2009-08-18,
User offline. Last seen 42 years 45 weeks ago.


Hello all, not sure if this is a 2.8 issue, but it seems strange ...
here it goes :

trait B

trait A {
  self: B =>

  def z(b: B)

  def b: B = this   // the compiler allows this

  z(this)  // but not this, wich is strange,

  z(b) // since this it allowed
}


 Cheers

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: Why is self type reference not equivalent to extends with r

On Wed, Jan 20, 2010 at 09:16:55PM -0500, Maxime Lévesque wrote:
> trait B
>
> trait A {
> self: B =>
>
> def z(b: B)
>
> def b: B = this // the compiler allows this
>
> z(this) // but not this, wich is strange,
>
> z(b) // since this it allowed
> }

That compiles as-is under both 2.7 and 2.8.

Maxime Lévesque
Joined: 2009-08-18,
User offline. Last seen 42 years 45 weeks ago.
Re: Why is self type reference not equivalent to extends with

  Hmm, I had a more complex case that does essentially this, I simplified it
too much, sorry for the noise...


If you add the following two lines, then it won't compile :

  val a0:A = error("")
 
  val b:B = a0  // < type mismatch error here

 any instance of A (or subtype) will have to implement trait B or it will never
be instantiated, so why would there be a type mismatch  ?


 Thanks
 
trait B

trait A {
  self: B =>

  def z(b: B)

  def b: B = this   // the compiler allows this

  z(this)  // but not this, wich is strange,

  z(b) // since this it allowed
}

  val a0:A = error("")
 
  val b:B = a0



2010/1/20 Paul Phillips <paulp@improving.org>
On Wed, Jan 20, 2010 at 09:16:55PM -0500, Maxime Lévesque wrote:
> trait B
>
> trait A {
>   self: B =>
>
>   def z(b: B)
>
>   def b: B = this   // the compiler allows this
>
>   z(this)  // but not this, wich is strange,
>
>   z(b) // since this it allowed
> }

That compiles as-is under both 2.7 and 2.8.

--
Paul Phillips      | Simplicity and elegance are unpopular because
Protagonist        | they require hard work and discipline to achieve
Empiricist         | and education to be appreciated.
i'll ship a pulp   |     -- Dijkstra

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: Why is self type reference not equivalent to extends with r

On Wed, Jan 20, 2010 at 10:14:57PM -0500, Maxime Lévesque wrote:
> val a0:A = error("")
> val b:B = a0

Self types vanish outside the instance. If you want A to be a B, A
should extend B.

Maxime Lévesque
Joined: 2009-08-18,
User offline. Last seen 42 years 45 weeks ago.
Re: Why is self type reference not equivalent to extends with

 I'm curious to know why,
what is wrong with the following reasoning :

 i)  all instances of A will have to also extend B in order to be instantiatable
    (they will need to implement B to not be abstract)
 ii) a reference of type A can only refer to an object that also extends B

 Does'n it follow that assigning an A to a reference of type B is always valid ?


2010/1/21 Paul Phillips <paulp@improving.org>
On Wed, Jan 20, 2010 at 10:14:57PM -0500, Maxime Lévesque wrote:
>   val a0:A = error("")
>   val b:B = a0

Self types vanish outside the instance.  If you want A to be a B, A
should extend B.

--
Paul Phillips      | A national political campaign is better than the
Stickler           | best circus ever heard of, with a mass baptism and
Empiricist         | a couple of hangings thrown in.
up hill, pi pals!  |     -- H. L. Mencken

ewilligers
Joined: 2008-08-20,
User offline. Last seen 3 years 17 weeks ago.
Re: Why is self type reference not equivalent to extends with r

Paul Phillips-3 wrote:
>
> On Wed, Jan 20, 2010 at 10:14:57PM -0500, Maxime Lévesque wrote:
>> val a0:A = error("")
>> val b:B = a0
>
> Self types vanish outside the instance. If you want A to be a B, A
> should extend B.
>

Or add a method:-

def asB: B = self
}

val b:B = a0.asB

Implicits could make this more convenient.

Maxime Lévesque
Joined: 2009-08-18,
User offline. Last seen 42 years 45 weeks ago.
Re: Why is self type reference not equivalent to extends with

  Yeah, that's what I ended up doing, I can
think of only one reason for self type references to be hidden from outside :
to loosen the coupling, i.e. code that depends (by referencing)
A, is not directly coupled to B. I'd be curious to know if there
are other reasons...

  Cheers


On Sat, Jan 23, 2010 at 2:43 AM, Eric Willigers <ewilligers@gmail.com> wrote:


Paul Phillips-3 wrote:
>
> On Wed, Jan 20, 2010 at 10:14:57PM -0500, Maxime Lévesque wrote:
>>   val a0:A = error("")
>>   val b:B = a0
>
> Self types vanish outside the instance.  If you want A to be a B, A
> should extend B.
>

Or add a method:-

 def asB: B = self
}

val b:B = a0.asB



Implicits could make this more convenient.

--
View this message in context: http://old.nabble.com/Why-is-self-type-reference-not-equivalent-to-extends-with-regards-to--type-compatibility-%28or-is-this-a-compiler-bug-%29-tp27251870p27284121.html
Sent from the Scala - User mailing list archive at Nabble.com.


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