On Jan 27, 7:18 pm, Reinier Zwitserloot <[email protected]> wrote:
> It's hard in scala, too, and that has reification).
Scala's type parameters are erased in just the same way as Java's.
Scala needs to retain compatibility so we're stuck with erasure.
Or are you talking about the experiment, unsupported, and likely to
murder kittens Manifest system? It's really a part of the reflection
system. It's not really a full solution to the problems people
complain about with type erasure. It's an implicit bridge between the
statically typed world that Scala normally inhabits and the
dynamically typed world of the reflection libraries. For instance,
here's how to solve the "new T" problem using reflection.
scala> import scala.reflect.Manifest
import scala.reflect.Manifest
scala> case class Factory[T](implicit manifest : Manifest[T]) {
| def newT = manifest.erasure.newInstance.asInstanceOf[T]
| }
defined class Factory
scala> val sfact = new Factory[String]
sfact: Factory[String] = Factory(java.lang.String)
scala> sfact.newT
res3: String =
scala> val lfact = new Factory[java.util.ArrayList[Int]]
lfact: Factory[java.util.ArrayList[Int]] = Factory(java.util.ArrayList
[int])
scala> lfact.newT
res4: java.util.ArrayList[Int] = []
Basically all it's saving me is the requirement to explicitly pass a
Class[T] as a constructor argument. But just like Class[T] based
solutions it's not type safe - you can specify types that don't have a
no-arg constructor
scala> val lfact = new Factory[java.lang.Integer]
lfact: Factory[java.lang.Integer] = Factory(java.lang.Integer)
scala> lfact.newT
java.lang.InstantiationException: java.lang.Integer
...
Frankly, the "new T" problem is better solved in Scala with a lazy
expression as a constructor parameter.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "The
Java Posse" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaposse?hl=en
-~----------~----~----~----~------~----~------~--~---