1

Supposing I wanted to implement a data-frame type thing in Scala, I might have a class that I could initialize a two column table like this:

val:foo = new MyDataFrame[String, Int]("a", "b")

And when I got a row, I'd expect it's type to be something like:

(String, Int)

That sort of thing is trivial to implement if I have a fixed number of columns, but what if I wanted to make a dataframe that would allow as many columns as I want.

val:foo = new MyDataFrame[Aaa, Bbb, ... Zzz]("ColA", "ColB", .... "ColZ")

Then I'd want my my row to have a type that looks like:

(Aaa, Bbb, ... Zzz)

My question is, how could I implement a class like MyDataFrame where the number of type arguments is not known in advance. I guess this is analogous to the use of Variable arguments when the number of regular inputs to a function cannot be known in advance.

Salim Fadhley
  • 6,975
  • 14
  • 46
  • 83
  • you can have var args .... but they should be of same type .... Do you need var args each one with different type ? – Nagarjuna Pamu Sep 15 '16 at 10:53
  • `case class A[T](list: T*)` something like this is possible.. but if you need different type for each item .. then i recommend using general super type and then type cast it to your custom type – Nagarjuna Pamu Sep 15 '16 at 10:55
  • @pamu, I need variable args each of whose are specified by the type of the class. If I was OK just returning a tuple of Any objects then I'd probably just use Python instead (joking). The idea of my question was to find out if I could make this kind of column-based dataframe but in a type-safe way. – Salim Fadhley Sep 15 '16 at 12:45
  • @pamu, taking the variable args is the easy bit - if I had a list of types I could presumably convert a bunch of Any objects to whatever the list of types required. The hard part was what I asked in the question - can I make my function return a row of the correc type. – Salim Fadhley Sep 15 '16 at 12:47
  • You could check out how `HList`s are implemented in Scala. – marstran Sep 15 '16 at 12:48
  • 2
    Or you could *use* `HList`. – Jasper-M Sep 15 '16 at 12:50
  • it is common to just generate a class for each arity – Łukasz Sep 15 '16 at 13:49
  • @Jasper-M - HLists seem to be just what I'm looking for. I asked another related question here: http://stackoverflow.com/questions/39584034/pick-out-the-nth-element-of-a-hlist-of-lists-and-return-that-value-as-a-hlist-of – Salim Fadhley Sep 20 '16 at 10:47

0 Answers0