0

In Scala/Spark DataFrame

    dfReduced.schema.fieldNames 

is a java String array (String[]). However,

    dfReduced.schema.fieldNames.asInstanceOf[Seq[String]]

throws

java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to scala.collection.Seq

Assigning same array to a Seq[String] is fine.

   val f3:Seq[String]=dfReduced.schema.fieldNames

As a Java programmer this surprises me as both would require casting in Java. Can someone explain why there is this distinction in Scala

(Note, I'm not being critical, I just want to understand Scala better)

Akash Sethi
  • 2,284
  • 1
  • 20
  • 40
Jake
  • 4,322
  • 6
  • 39
  • 83
  • 2
    The difference is `run-time` type cast versus `compile-time` type ascription. This StackOverflow [link](https://stackoverflow.com/a/3412235/6316508) might be of interest to you. – Leo C Sep 13 '17 at 04:32

1 Answers1

3

The reason why val f3:Seq[String]=dfReduced.schema.fieldNames this is working is because In Scala there is implicit conversion available than can cast the Array[T] to Seq[T] implicitly

In Java there is no such type of implicit casting available.

As Leo C mention in comment The difference is run-time type cast versus compile-time type ascription. For more info you can refer to this link.

Hope this clears your dough

Thanks

Akash Sethi
  • 2,284
  • 1
  • 20
  • 40