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

Existential type understanding

3 replies
Alefas
Joined: 2009-06-23,
User offline. Last seen 33 weeks 5 days ago.
Hi guys,

I have problems with understanding of existential types.
object Problem {
  class A[T]
  class B extends A[B]
  def goo[T <: A[T]](y: T): T = y
  type Y = T forSome {type T <: A[T]}
  val x: Y = null
  goo(x)
  goo[Y](x)
}
In this code goo(x) is compiled, and goo[Y](x) is not. I want to understand why. My vision is that it is a bug.

Original problem is trying to understand how to resolve most specific alternative in this case:
object Problem {
  class A[T]
  class B extends A[B]
  def goo[T <: A[T]](y: T): T = y
  def goo[T <: A[T]](y: T*): T = y

  goo(new B)
}

Best regards,
Alexander Podkhalyuzin.
dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: Existential type understanding

AFAIK, T <: A[T] is not the same type as T forSome { type T <: A[T]
}. One is existential, the other is not.

On Thu, Dec 15, 2011 at 10:40, Alexander Podkhalyuzin
wrote:
> Hi guys,
>
> I have problems with understanding of existential types.
>
> object Problem {
> class A[T]
> class B extends A[B]
> def goo[T <: A[T]](y: T): T = y
> type Y = T forSome {type T <: A[T]}
> val x: Y = null
> goo(x)
> goo[Y](x)
> }
>
> In this code goo(x) is compiled, and goo[Y](x) is not. I want to understand
> why. My vision is that it is a bug.
>
> Original problem is trying to understand how to resolve most specific
> alternative in this case:
>
> object Problem {
> class A[T]
> class B extends A[B]
> def goo[T <: A[T]](y: T): T = y
> def goo[T <: A[T]](y: T*): T = y
>
> goo(new B)
> }
>
>
> Best regards,
> Alexander Podkhalyuzin.

DaveScala
Joined: 2011-03-18,
User offline. Last seen 1 year 21 weeks ago.
Re: Existential type understanding

You could make A covariant in T.

object Problem {
class A[+T]
class B extends A[B]
def goo[T <: A[T]](y: T): T = y
type Y = T forSome {type T <: A[T]}
val x: Y = null
goo(x)
goo[Y](x)
}

It compiles but I don't know why and what it means.

On 15 dec, 13:40, Alexander Podkhalyuzin
wrote:
> Hi guys,
>
> I have problems with understanding of existential types.
>
> object  Problem {
>    class  A[T]
>    class  Bextends  A[B]
>    def  goo[T<: A[T]](y: T): T = y
>    type  Y  = T forSome {type  T  <: A[T]}
>    val  x: Y =null
>    goo(x)
>    goo[Y](x)
>
> }
>
> In this code goo(x) is compiled, and goo[Y](x) is not. I want to
> understand why. My vision is that it is a bug.
>
> Original problem is trying to understand how to resolve most specific
> alternative in this case:
>
> object  Problem {
>    class  A[T]
>    class  Bextends  A[B]
>    def  goo[T<: A[T]](y: T): T = y
>    def  goo[T<: A[T]](y: T*): T = y
>
>    goo(new  B)
>
> }
>
> Best regards,
> Alexander Podkhalyuzin.

Alefas
Joined: 2009-06-23,
User offline. Last seen 33 weeks 5 days ago.
Re: Existential type understanding

Yes, I actually discovered, what type inferred for goo(x), it's
something called "T where T <: A[T]", and I see that it is not
existential type. All I can assume is that it's internal compiler type
(skolemized type?), but usually you shouldn't be able to get this type
explicit. And even it's some internal compiler type (or something else)
and it's not a bug, I still can't understand what reason to change
existential type to something different?

Best regards,
Alexander Podkhalyuzin.

15.12.2011 20:54, Daniel Sobral пишет:
> AFAIK, T<: A[T] is not the same type as T forSome { type T<: A[T]
> }. One is existential, the other is not.
>
> On Thu, Dec 15, 2011 at 10:40, Alexander Podkhalyuzin
> wrote:
>> Hi guys,
>>
>> I have problems with understanding of existential types.
>>
>> object Problem {
>> class A[T]
>> class B extends A[B]
>> def goo[T<: A[T]](y: T): T = y
>> type Y = T forSome {type T<: A[T]}
>> val x: Y = null
>> goo(x)
>> goo[Y](x)
>> }
>>
>> In this code goo(x) is compiled, and goo[Y](x) is not. I want to understand
>> why. My vision is that it is a bug.
>>
>> Original problem is trying to understand how to resolve most specific
>> alternative in this case:
>>
>> object Problem {
>> class A[T]
>> class B extends A[B]
>> def goo[T<: A[T]](y: T): T = y
>> def goo[T<: A[T]](y: T*): T = y
>>
>> goo(new B)
>> }
>>
>>
>> Best regards,
>> Alexander Podkhalyuzin.
>
>

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