I am trying to write some convenient functions in Scala for reading in arrays of values.
I started with a function that converts a string like "1 1 2 3 5 8" into an [Int] array:
def readInts(in: String) = in.split(" ").map(_.toInt)
This works fine, except if I want to read not only ints, but also Longs or BigInts or Doubles, I need to define a function for each one that seems wasteful (especially if I generalize to reading in matrices or other composite data )
I would like to write one polymorphic function as follows:
def readArray[A](in: String) = in.split(" ").map(_.to[A])
As far as I understand, this is not possible because the String class does not have a polymorphic "to" method. Good; Instead, I will try to define it as a helper method:
def to[A](in: String) = ???
It looks like I need to conditionally define a method for a type parameter - if A is Int, then call in.toInt ; if A is Double, call in.toDouble ; if A is Tuple2 [Int, Int], call the helper method toTupleOfInts(in) . As far as I know, this is also impossible.
In another functional language that I know of, Haskell, this problem is handled by the Read class, which defines the polymorphic read function, which converts from a string to the desired data type.
What is the idiomatic way to do this (i.e. write polymorphic input functions) in Scala?