# ClassTag for parametrized Array

**URL:** <https://users.scala-lang.org/t/classtag-for-parametrized-array/3894>\
**Category:** Question\
**Created:** [January 11, 2019, 5:27am UTC](https://users.scala-lang.org/t/classtag-for-parametrized-array/3894 "2019-01-11T05:27:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mghildiy](https://users.scala-lang.org/user_avatar/users.scala-lang.org/mghildiy/32/2658_2.png) [@mghildiy](https://users.scala-lang.org/u/mghildiy)\
**Post date:** [January 11, 2019, 5:27am UTC](https://users.scala-lang.org/t/classtag-for-parametrized-array/3894/1 "2019-01-11T05:27:08Z")

</div>

In following code, I need to make use of ClassTag.

> def init[A:ClassTag](l: List[A]): Array[A] = {  
> def go(acc: Array[A], li: List[A]): Array[A] = {  
> li match {  
> case Cons(h, Cons(g, Nil)) =\> acc :+ h  
> case Cons(h, t) =\> go(acc :+ h, t)  
> }  
> }  
> go(Array.empty[A], l)  
> }

Why is ClassTag needed here? Not having it gives compilation error

> No ClassTag available for A

For List, ClassTag is not needed.

---

<div class="post-metadata">

**Author:** ![crater2150](https://users.scala-lang.org/user_avatar/users.scala-lang.org/crater2150/32/1011_2.png) [@crater2150](https://users.scala-lang.org/u/crater2150)\
**Post date:** [January 11, 2019, 9:40am UTC](https://users.scala-lang.org/t/classtag-for-parametrized-array/3894/2 "2019-01-11T09:40:31Z")

</div>

This is caused by how Arrays are represented on the JVM. You don’t need a ClassTag for list, because it is a normal class / trait, with a generic parameter. When you have a `List[String]` and a `List[Int]`, type erasure will cause them to have the same class at runtime. All type checks are done at compile time. In Java, you are allowed to leave generic parameters off, getting an unchecked “raw type”, which is what you’ll have at runtime.

Arrays on the other hand have different runtime classes on the JVM. Array types are not erased, so an `Array[String]` and and `Array[Int]` are different classes at runtime. For that reason, you need to know the type at runtime to instantiate the class. This is what the `ClassTag` does, it holds the type at runtime.

In Java, the difference is a bit more obvious, because of the different syntax for Arrays compared to generics, and because Arrays can have primitive types, which Java generics can not.

---

<div class="post-metadata">

**Author:** ![curoli](https://users.scala-lang.org/user_avatar/users.scala-lang.org/curoli/32/1753_2.png) [@curoli](https://users.scala-lang.org/u/curoli)\
**Post date:** [January 11, 2019, 3:15pm UTC](https://users.scala-lang.org/t/classtag-for-parametrized-array/3894/3 "2019-01-11T15:15:37Z")

</div>

The uniform syntax for Array[A] and List[A] obscures that Array is special. For the JVM, array types are special builtin types of known element type (long predating other generic types), so if you have Array[String], the JVM will know that it is an Array[String], while for List[String], it will only know it is List.

That means the JVM can only create an Array if it knows the element type (although there are situations where an Array[A] gets turned into an Array[AnyRef]), but for other parametrized types, the parameter type will not be known anyway .

\*\*scala\> List(“yo”).getClass.getCanonicalName  
res0: String = scala.collection.immutable.$colon$colon

> Array(“yo”).getClass.getCanonicalName  
> res1: String = java.lang.String\*\*

In Java, it is more obvious that Arrays are different, because Array[A] is written A, while List[A] is written List.

---

<div class="post-metadata">

**Author:** ![ashwinbhaskar](https://users.scala-lang.org/user_avatar/users.scala-lang.org/ashwinbhaskar/32/867_2.png) [@ashwinbhaskar](https://users.scala-lang.org/u/ashwinbhaskar)\
**Post date:** [January 12, 2019, 2:14am UTC](https://users.scala-lang.org/t/classtag-for-parametrized-array/3894/4 "2019-01-12T02:14:16Z")

</div>

> [@crater2150](#):
>
> When you have a `List[String]` and a `List[Int]` , type erasure will cause them to have the same class at runtime.

That means will both be **List of Any** at run time. How is it ensured at run time that we get a **String** and an **Int** when we access those lists? Wouldn’t they return ' **Any’**?

---

<div class="post-metadata">

**Author:** ![Jasper-M](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jasper-m/32/872_2.png) [@Jasper-M](https://users.scala-lang.org/u/Jasper-M)\
**Post date:** [January 12, 2019, 10:59am UTC](https://users.scala-lang.org/t/classtag-for-parametrized-array/3894/5 "2019-01-12T10:59:17Z")

</div>

When you take an element out of a `List[String]` the compiler inserts a cast to `String` in the bytecode, which it can prove is safe as long as you didn’t do any unsafe casts yourself.
