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

Overcome type erasures with manifest.

4 replies
edmondo1984
Joined: 2011-09-14,
User offline. Last seen 28 weeks 3 days ago.
Dear All,
I have the following use case:
Trait A[T]
Class B extends A[Int]Class C extends A[Long] Class E extends A[Double]
Is it possible to use manifests so that accessing an element of IndexedSeq(new B, new C,new E) returns the right type of element and not justtrait A[_] ?
Best Regards Edmondo


H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
Re: Overcome type erasures with manifest.

no. manifests capture the type of a generic parameter. what you need is a hlist

-------- Original-Nachricht --------
> Datum: Mon, 23 Jan 2012 09:36:27 +0100
> Von: Edmondo Porcu
> An: scala-user
> Betreff: [scala-user] Overcome type erasures with manifest.

> Dear All,
>
> I have the following use case:
>
> Trait A[T]
>
> Class B extends A[Int]
> Class C extends A[Long]
> Class E extends A[Double]
>
> Is it possible to use manifests so that accessing an element of
> IndexedSeq(new B, new C,new E) returns the right type of element and not
> just
> trait A[_] ?
>
> Best Regards
> Edmondo

edmondo1984
Joined: 2011-09-14,
User offline. Last seen 28 weeks 3 days ago.
Re: Overcome type erasures with manifest.
Are that a part of Scala library? Where I can get a reliable implementation for that?
Best Regards

2012/1/23 Dennis Haupt <h-star@gmx.de>
no. manifests capture the type of a generic parameter. what you need is a hlist

-------- Original-Nachricht --------
> Datum: Mon, 23 Jan 2012 09:36:27 +0100
> Von: Edmondo Porcu <edmondo.porcu@gmail.com>
> An: scala-user <scala-user@googlegroups.com>
> Betreff: [scala-user] Overcome type erasures with manifest.

> Dear All,
>
> I have the following use case:
>
> Trait A[T]
>
> Class B extends A[Int]
> Class C extends A[Long]
> Class E extends A[Double]
>
> Is it possible to use manifests so that accessing an element of
> IndexedSeq(new B, new C,new E) returns the right type of element and not
> just
> trait A[_] ?
>
> Best Regards
> Edmondo

Kevin Wright 2
Joined: 2010-05-30,
User offline. Last seen 26 weeks 4 days ago.
Re: Overcome type erasures with manifest.
Try this one on for size: https://github.com/milessabin/shapeless

On 23 January 2012 11:28, Edmondo Porcu <edmondo.porcu@gmail.com> wrote:
Are that a part of Scala library? Where I can get a reliable implementation for that?
Best Regards

2012/1/23 Dennis Haupt <h-star@gmx.de>
no. manifests capture the type of a generic parameter. what you need is a hlist

-------- Original-Nachricht --------
> Datum: Mon, 23 Jan 2012 09:36:27 +0100
> Von: Edmondo Porcu <edmondo.porcu@gmail.com>
> An: scala-user <scala-user@googlegroups.com>
> Betreff: [scala-user] Overcome type erasures with manifest.

> Dear All,
>
> I have the following use case:
>
> Trait A[T]
>
> Class B extends A[Int]
> Class C extends A[Long]
> Class E extends A[Double]
>
> Is it possible to use manifests so that accessing an element of
> IndexedSeq(new B, new C,new E) returns the right type of element and not
> just
> trait A[_] ?
>
> Best Regards
> Edmondo




milessabin
Joined: 2008-08-11,
User offline. Last seen 33 weeks 3 days ago.
Re: Overcome type erasures with manifest.

On Mon, Jan 23, 2012 at 11:46 AM, Kevin Wright wrote:
> Try this one on for size: https://github.com/milessabin/shapeless

With the proviso that shapeless is highly experimental ;-)

Sample REPL session ...

scala> import shapeless._ ; import Nat._
import shapeless._
import Nat._

scala> trait A[T]
defined trait A

scala> class B extends A[Int] ; class C extends A[Long] ; class E
extends A[Double]
defined class B
defined class C
defined class E

scala> val l = new B :: new C :: new E :: HNil
l: shapeless.::[B,shapeless.::[C,shapeless.::[E,shapeless.HNil]]] =
B@1d333c99 :: C@ecf6fc9 :: E@54b3fcb7 :: HNil

scala> l(_0)
res0: B = B@1d333c99

scala> l(_1)
res1: C = C@ecf6fc9

scala> l(_2)
res2: E = E@54b3fcb7

scala> l.toList[A[_]]
res3: List[A[_]] = List(B@1d333c99, C@ecf6fc9, E@54b3fcb7)

Cheers,

Miles

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