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

R: Re: Overcome type erasures with manifest.

3 replies
edmondo1984
Joined: 2011-09-14,
User offline. Last seen 28 weeks 3 days ago.

Is there anything like an HMap? Not an hashmap :)
------Messaggio originale------
Da: Miles Sabin
A:Kevin Wright
Cc:Edmondo
Cc:Dennis Haupt
Cc:scala-user@googlegroups.com
Oggetto: Re: [scala-user] Overcome type erasures with manifest.
Inviato: 23 Gen 2012 12:52

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

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

On Mon, Jan 23, 2012 at 11:56 AM, wrote:
> Is there anything like an HMap? Not an hashmap :)

You mean something like this?

http://goo.gl/P96YT

Cheers,

Miles

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

yes. it's called "class".
do you want groovy/ruby like extensible classes, just with type safety?

-------- Original-Nachricht --------
> Datum: Mon, 23 Jan 2012 11:56:41 +0000
> Von: edmondo.porcu@gmail.com
> An: "Miles Sabin" , "Kevin Wright"
> CC: "Dennis Haupt" , scala-user@googlegroups.com
> Betreff: R: Re: [scala-user] Overcome type erasures with manifest.

> Is there anything like an HMap? Not an hashmap :)
> ------Messaggio originale------
> Da: Miles Sabin
> A:Kevin Wright
> Cc:Edmondo
> Cc:Dennis Haupt
> Cc:scala-user@googlegroups.com
> Oggetto: Re: [scala-user] Overcome type erasures with manifest.
> Inviato: 23 Gen 2012 12:52
>
> 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
>

virtualeyes
Joined: 2011-12-19,
User offline. Last seen 33 weeks 4 days ago.
Re: R: Re: Overcome type erasures with manifest.

Groovy beans would be rather convenient compared to case classes,
black boxes that they are (in regard to reflection)

Perhaps in 2.10 new Scala reflection will allow for straight forward,
non-external library dependent, access to class property names & types
at run time.

Then you could do something useful, like validate servlet params map
against the properties of a to-be-created case class instance without
having to resort to Scalaz or other doctorate-required library to act
as a proxy for creating your objects.

Sure you lose compile time safety, but then again, your application
and its domain classes have become so much easier to work with. Pick
your poison.

Take with a grain of salt, just a couple of months into Scala, coming
from Groovy, where run time freedom creates its own basket of
"joy" (typos, untraceable errors, no generics, etc.)

On Jan 23, 1:33 pm, "Dennis Haupt" wrote:
> yes. it's called "class".
> do you want groovy/ruby like extensible classes, just with type safety?
>
> -------- Original-Nachricht --------
>
>
>
>
>
>
>
> > Datum: Mon, 23 Jan 2012 11:56:41 +0000
> > Von: edmondo.po...@gmail.com
> > An: "Miles Sabin" , "Kevin Wright"
> > CC: "Dennis Haupt" , scala-user@googlegroups.com
> > Betreff: R: Re: [scala-user] Overcome type erasures with manifest.
> > Is there anything like an HMap? Not an hashmap :)
> > ------Messaggio originale------
> > Da: Miles Sabin
> > A:Kevin Wright
> > Cc:Edmondo
> > Cc:Dennis Haupt
> > Cc:scala-user@googlegroups.com
> > Oggetto: Re: [scala-user] Overcome type erasures with manifest.
> > Inviato: 23 Gen 2012 12:52
>
> > 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
>
> > --
> > Miles Sabin
> > tel: +44 7813 944 528
> > gtalk: mi...@milessabin.com
> > skype: milessabin
> > g+:http://www.milessabin.com
> >http://twitter.com/milessabin
> >http://www.chuusai.com/
>
> > Inviato da BlackBerry(R) Wireless Handheld

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