Q:

Spark Datasets and variance

Context
I have created a function that takes a Dataset[MyCaseClass] and returns an array of the elements of one of its columns
 def columnToArray(ds: Dataset[MyCaseClass], columnName: String): Array[String] = {
   ds
     .select(columnName)
     .rdd
     .map(row => row(0).toString)
     .collect
 }

It works fine with case class
case class MyCaseClass(
  fieldA: String,
  fieldB: String,
  fieldC: String
)

Problem
I need to reuse the functionality with a Dataset of some other type.  In an attempt to generalize, I declared the Dataset's type as AnyRef but when I use it with the aforementioned case class I get the following error

type mismatch;  found   : org.apache.spark.sql. Dataset[com.my.package. MyCaseClass]  required: org.apache.spark.sql. Dataset[AnyRef] Note: com.my.package. MyCaseClass <: AnyRef, but class Dataset is invariant in type T. You may wish to define T as +T instead. ( SLS 4.5)

I suppose that type T mentioned in the error is the type of Dataset so I cannot change its variance.  Is there some other way I can generalize this function somehow? 

A:

For this particular function exact type of the Dataset doesn't matter, so I'd simply:
def columnToArray(ds: Dataset[_], columnName: String): Array[String] = {
 ds
   .select(columnName)
   .rdd
   .map(row => row(0).toString)
   .collect
}

