0

Is it possible to determine type of Seq[A] in Scala 2.11.2?

For example:

val fruit = List("apples", "oranges", "pears")
val nums = List(1, 2, 3, 4) 

I want to print type of Seq something like this:

scala> def printType[A](xs: Seq[A]): Unit = xs match {
     | case x: List[String] => println("String")
     | case y: List[Int] => println("Int")
     | }

<console>:8: warning: non-variable type argument String in type pattern List[Str
ing] (the underlying of List[String]) is unchecked since it is eliminated by era
sure
       case x: List[String] => println("String")
               ^
<console>:9: warning: non-variable type argument Int in type pattern List[Int] (
the underlying of List[Int]) is unchecked since it is eliminated by erasure
       case y: List[Int] => println("Int")
               ^
<console>:9: warning: unreachable code
       case y: List[Int] => println("Int")
                                   ^
printType: [A](xs: Seq[A])Unit

p.s. I'm new in Scala.

UPDATE:

Is there solution without using Reflection API?

Ir3000
  • 80
  • 5

1 Answers1

0

you could use type tags, see the accepted answer to this question for a detailed example similar to your own: Scala: What is a TypeTag and how do I use it?

Community
  • 1
  • 1
Angelo Genovese
  • 3,398
  • 17
  • 23
  • 2
    please, don't answer just linking to other answers. Either mark the question as duplicate or comment. – Gabriele Petronella Aug 19 '14 at 17:06
  • Apologies, but neither seemed appropriate. I didn't think they were duplicates since the linked question is asking for an example of using type tags. My answer to his question was "You could use TypeTags" the link is a reference which provides a solid example and happens to be an SO answer. – Angelo Genovese Aug 19 '14 at 17:15
  • 1
    link-only answer are discouraged in any case: the answer should be valuable by itself and links should only provide additional references. If you don't think it's a duplicate then please write down a solution yourself and use the link as a reference. – Gabriele Petronella Aug 19 '14 at 17:19