in Education by
It's a sad fact of life on Scala that if you instantiate a List[Int], you can verify that your instance is a List, and you can verify that any individual element of it is an Int, but not that it is a List[Int], as can be easily verified: scala> List(1,2,3) match { | case l : List[String] => println("A list of strings?!") | case _ => println("Ok") | } warning: there were unchecked warnings; re-run with -unchecked for details A list of strings?! The -unchecked option puts the blame squarely on type erasure: scala> List(1,2,3) match { | case l : List[String] => println("A list of strings?!") | case _ => println("Ok") | } :6: warning: non variable type-argument String in type pattern is unchecked since it is eliminated by erasure case l : List[String] => println("A list of strings?!") ^ A list of strings?! Why is that, and how do I get around it? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
This answer uses the Manifest-API, which is deprecated as of Scala 2.10. Please see answers below for more current solutions. Scala was defined with Type Erasure because the Java Virtual Machine (JVM), unlike Java, did not get generics. This means that, at run time, only the class exists, not its type parameters. In the example, JVM knows it is handling a scala.collection.immutable.List, but not that this list is parameterized with Int. Fortunately, there's a feature in Scala that lets you get around that. It’s the Manifest. A Manifest is class whose instances are objects representing types. Since these instances are objects, you can pass them around, store them, and generally call methods on them. With the support of implicit parameters, it becomes a very powerful tool. Take the following example, for instance: object Registry { import scala.reflect.Manifest private var map= Map.empty[Any,(Manifest[_], Any)] def register[T](name: Any, item: T)(implicit m: Manifest[T]) { map = map.updated(name, m -> item) } def get[T](key:Any)(implicit m : Manifest[T]): Option[T] = { map get key flatMap { case (om, s) => if (om <:< m) Some(s.asInstanceOf[T]) else None } } } scala> Registry.register("a", List(1,2,3)) scala> Registry.get[List[Int]]("a") res6: Option[List[Int]] = Some(List(1, 2, 3)) scala> Registry.get[List[String]]("a") res7: Option[List[String]] = None When storing an element, we store a "Manifest" of it too. A Manifest is a class whose instances represent Scala types. These objects have more information than JVM does, which enable us to test for the full, parameterized type. Note, however, that a Manifest is still an evolving feature. As an example of its limitations, it presently doesn't know anything about variance, and assumes everything is co-variant. I expect it will get more stable and solid once the Scala reflection library, presently under development, gets finished.

Related questions

0 votes
    In scala, it is OK to convert a variable in the Seq, but if I construct the Seq with :: it ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    I have a 2 column (1 int and 1 double) dataframe "fit_comparison", of predicted values and linear ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    I have a generic method which a generic type parameter T which is a subclass of MyClass. Inside that method, I want ... of type erasure): object Demo extends App { def myMethod[T...
asked Jun 30, 2022 in Education by JackTerrance
0 votes
    I am working on a project with spark and scala and I am new to both but with lot of help from ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 19, 2022 in Education by JackTerrance
0 votes
    In Mercury I can use: A = B^some_field := SomeValue to bind A to a copy of B, except that ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 4, 2022 in Education by JackTerrance
0 votes
    In Mercury I can use: A = B^some_field := SomeValue to bind A to a copy of B, except that ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 4, 2022 in Education by JackTerrance
0 votes
    In Mercury I can use: A = B^some_field := SomeValue to bind A to a copy of B, except that ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 4, 2022 in Education by JackTerrance
0 votes
    I work with Spark often, and it would save me a lot of time if the compiler could ensure that a type is serializable. ... T to be serializable } It's not enough to constrain T...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    I'm struggling to get custom defined mapping between my case classes and database tables due to type ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 5, 2022 in Education by JackTerrance
0 votes
    I have to retrieve Derived class objects stored in a Map given the respective class name as key. As ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 27, 2022 in Education by JackTerrance
0 votes
    I have to retrieve Derived class objects stored in a Map given the respective class name as key. As ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 27, 2022 in Education by JackTerrance
0 votes
    How can I convert an RDD to a dataframe? I converted a data frame to rdd using .rdd. After processing it I ... convert it back to rdd Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    We are building sentiment analysis application and we converted our tweets dataframe to an array. We created another array ... .txt").getLines.toArray var happyCount=0 for (e...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    We are building sentiment analysis application and we converted our tweets dataframe to an array. We created another array ... .txt").getLines.toArray var happyCount=0 for (e...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I am working on spark project using Scala. I need to print each element of a list named 'c' ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
...