blue-sky
blue-sky

Reputation: 53826

Printing type and contents to console

In the Scala repl if I evaluate :

val lines : String = ("a1 , test1 , test2 , a2 , test3 , test4")
    lines.split(",").grouped(3).toList 

I receive this on save :

//> res0: List[Array[String]] = List(Array("a1 ", " test1 ", " test2 "), Array("
                                                  //|  a2 ", " test3 ", " test4"))

I would like to be able to print this information to the console, the value of res0

So something like printTypeInformation(lines.split(",").grouped(3).toList) will print same value as res0 above. I think I could achieve this for above type by iterating over the List printing the values and type information. But is there a more generic method so that this information can be printed for any type ?

Upvotes: 0

Views: 942

Answers (2)

4lex1v
4lex1v

Reputation: 21557

The answer is close to the one from om-nom-nom, but as of scala 2.10 i think better to use TypeTags:

scala> "a1 , test1 , test2 , a2 , test3 , test4"
res3: String = a1 , test1 , test2 , a2 , test3 , test4

scala> res3.split(",").grouped(3).toList
res4: List[Array[String]] = List(Array("a1 ", " test1 ", " test2 "), Array(" a2 ", " test3 ", " test4"))

scala> def typped[T: TypeTag](obj: T) = typeOf[T]
typped: [T](obj: T)(implicit evidence$1: reflect.runtime.universe.TypeTag[T])reflect.runtime.universe.Type

scala> typped(res4)
res5: reflect.runtime.universe.Type = scala.List[scala.Array[String]]

Upvotes: 1

om-nom-nom
om-nom-nom

Reputation: 62835

As pointed out by @Jatin , you can do this with manifests:

def manOf[T: Manifest](t: T): Manifest[T] = manifest[T]
val lines : String = ("a1 , test1 , test2 , a2 , test3 , test4")
val xs = lines.split(",").grouped(3).toList
println(manOf(xs))
// scala.collection.immutable.List[Array[java.lang.String]]

Upvotes: 1

Related Questions