Scala collection filter by type?

Pay attention to the compiler warnings, and add -unchecked your scala command line options M:\>scala -unchecked Welcome to Scala version 2.8.0. Final (Java HotSpot(TM) Client VM, Java 1.6.0_21) . Type in expressions to have them evaluated.

Type :help for more information. Scala> class CollectionExtensionE(coll : TraversableE) { | | def onlyInstancesOfSpecialE special}). AsInstanceOfTraversableSpecialE | } | } :8: warning: abstract type SpecialE in type pattern SpecialE is unchecked since it is eliminated by erasure coll.

Collect({case special : SpecialE => special}). AsInstanceOfTraversableSpecialE ^ defined class CollectionExtension The warning means that the best the compiler can do is equivalent to: coll. Collect({case special : AnyRef => special}).

AsInstanceOfTraversable_ For a more detailed explanation of type erasure, and ways you can work around it with Manifests, see: http://stackoverflow.com/questions/tagged/type-erasure+scala.

Pay attention to the compiler warnings, and add -unchecked your scala command line options. M:\>scala -unchecked Welcome to Scala version 2.8.0. Final (Java HotSpot(TM) Client VM, Java 1.6.0_21) .

Type in expressions to have them evaluated. Type :help for more information. Scala> class CollectionExtensionE(coll : TraversableE) { | | def onlyInstancesOfSpecialE special}).

AsInstanceOfTraversableSpecialE | } | } :8: warning: abstract type SpecialE in type pattern SpecialE is unchecked since it is eliminated by erasure coll. Collect({case special : SpecialE => special}). AsInstanceOfTraversableSpecialE ^ defined class CollectionExtension The warning means that the best the compiler can do is equivalent to: coll.

Collect({case special : AnyRef => special}). AsInstanceOfTraversable_ For a more detailed explanation of type erasure, and ways you can work around it with Manifests, see: http://stackoverflow.com/questions/tagged/type-erasure+scala.

As others have pointed out, manifests can rescue you. Here's an example of how, restricting ourselves to non-primitives, and assuming we don't want to store manifests in our collections but instead use reflection on the spot to figure things out: class CollectionExtensionE special }). AsInstanceOfTraversableSpecialE } } and here it is in action: scala> val ce = new CollectionExtension(List(Some(1),Some(5),"This","Fox")) ce: CollectionExtensionjava.lang.

Object = CollectionExtension@1b3d4787 scala> val opts = ce. OnlyInstancesOfSome_ opts: TraversableSome_ = List(Some(1), Some(5)) scala> val strings = ce. OnlyInstancesOfString strings: TraversableString = List(This, Fox).

Scala runs on the JVM, which unfortunately erases type parameters at runtime: en.wikipedia.org/wiki/Generics_in_Java#T.... In your first example, you give the type in a non-erased position and so the runtime code can do the comparison. In the second example, the SpecialE type is erased, and hence the code will return everything.

You can use scala's Manifests to regain some of the information lost by type erasure: import scala.reflect. ClassManifest class CollectionsExtensionE e}). AsInstanceOfTraversableSpecialE } }.

As the warning say: :14: warning: abstract type SpecialE in type pattern SpecialE is unchecked since it is eliminated by erasure coll. Collect({case special : SpecialE => special}). AsInstanceOfTraversableSpecialE Let's see the implementation of collect: def collectB, That(pf: PartialFunctionA, B)(implicit bf: CanBuildFromRepr, B, That): That = { val be = bf(repr) for (x d}" the compiler knows exactly what type you are talking about: D.

On the other hand, when you write coll. Collect({case special : SpecialE => special}), the compiler doesn't know what type SpecialE, because SpecialE is just a type parameter. So it can't generate code that knows what SpecialE is, and, at run-time, there's no SpecialE anymore -- the bytecode just uses java.lang.Object.

I want to get a subcollection of an existing collection that only contains elements of a specific type. But when I try to extend the collection classes with a method "onlyInstancesOfType" this does not work. I get an error that .

Size returned 4 and not 2.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions