0

Lets say I want to construct a list of records, where each record consists of some x number of fields. However, the number of fields and type of the fields are not known at compile time. Only at run-time, we know both the number of fields and type of each field. So to represent this list, I use an List[Array[Any]].

A user should be able to find a record from this list with min (max) value for a certain field.

Here is a highly simplified sample code:

class Data(val colValues: List[Array[Any]]) {
  def min(i: Int): Array[Any] = {
    colValues.minBy { _(i) }
  }
}

A user should be able to use it this way:

val rawData = List(Array("a", 20, "z", "m", 3.0), Array("b", 10, "y", "f", 4.0), Array("c", 40, "z", "m", 2.0))
val d =  new Data(rawData)
val m1 = d.min(1)
val m2 = d.min(4)

The above code will not work. Scala gives this error:

  • not enough arguments for method minBy: (implicit cmp: Ordering[Any])Array[Any]. Unspecified value parameter cmp.
  • No implicit Ordering defined for Any.

In fact, as expected, Scala complains anytime I call a higher-order method on this list such as maxBy, sortWith, and sum.

So I modified the code to this:

class Data(val colValues: List[Array[Any]]) {
  def withType(x: Any) = x match {
          case i: Int => i
          case l: Long => l
          case f: Float => f
          case d: Double => d
  }

  def min(i: Int): Array[Any] = {
    colValues.minBy { x:Array[Any] => withType(x(i)) }
  }
}

This code compiles and runs as expected. However, I feel that there must be a more elegant solution than this. Plus, the above code will not work if the min method is called with an index of a field that is of type String.

Is there a better way to handle type erasure and type safety in case of an Array[Any], where the array stores different types of elements and whose types are known only at run-time?

In addition, is there a better data type than List[Array[Any]] for representing a list of records whose field types (and number) are known only at run-time?

Thank you.

1
  • 2
    The reason code above doesn't work is not type erasure.
    – om-nom-nom
    Apr 30, 2013 at 18:58

1 Answer 1

2

Your problem here is that you don't have a correct Ordering instance in implicit scope. The reason one doesn't exist is Ordering[Any] is non-sensical. Even if we had such an instance what purpose would it serve, what does it mean to compare a String and Int? Or an Object and a List? You can make up an ordering but it would be useless because there is no way to obtain anything interesting about something of the type Any. There are some complicated ways to achieve what you want, but this seems indicative of a design problem, you may want to try to rethink your approach.

1
  • Please remember that the type is known at run-time and for each position in a record, the type is the same anywhere in the list. In other words, if the first field is of type Int in record #1, the first field will be of type Int in all other records. So we will never compare a String with Int. Apr 30, 2013 at 20:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.