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 justtrait A[_] ?
Best Regards Edmondo


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

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 [at] gmx [dot] 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 [dot] porcu [at] gmail [dot] com>
> An: scala-user <scala-user [at] googlegroups [dot] 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

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 [dot] porcu [at] gmail [dot] 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 [at] gmx [dot] 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 [dot] porcu [at] gmail [dot] com>
> An: scala-user <scala-user [at] googlegroups [dot] 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




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 © 2013 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland