Scala recursive type and type constructor implementation

Viewed 591

I have a situation where I need a method that can take in types:

Array[Int]
Array[Array[Int]]
Array[Array[Array[Int]]]
Array[Array[Array[Array[Int]]]]
etc...

let's call this type RAI for "recursive array of ints"

def make(rai: RAI): ArrayPrinter = { ArrayPrinter(rai) }

Where ArrayPrinter is a class that is initialized with an RAI and iterates through the entire rai (let's say it prints all the values in this Array[Array[Int]])

val arrayOfArray: Array[Array[Int]] = Array(Array(1, 2), Array(3, 4))
val printer: ArrayPrinter[Array[Array[Int]]] = make(arrayOfArray)
printer.print_! // prints "1, 2, 3, 4" 

It can also return the original Array[Array[Int]] without losing any type information.

val arr: Array[Array[Int]] = printer.getNestedArray() 

How do you implement this in Scala?

2 Answers
Related