If I have these 2 or N case class
es as follows:
case class dbep1 (a1:String, b1:String, ts1: Integer)
case class dbep2 (a2:String, b2:String, ts2: Integer)
var dbep1D = new ArrayBuffer[dbep1]()
dbep1D = ArrayBuffer(dbep1("Mark", "Hamlin", 2), dbep1("Kumar", "XYZ", 3), dbep1("Tom", "Poolsoft", 4))
var dbep2D = new ArrayBuffer[dbep2]()
dbep2D = ArrayBuffer(dbep2("Pjotr", "Hamming", 4), dbep2("Kumar", "ABNC", 7), dbep2("Tom", "Gregory", 3))
How can I write a def that accepts both of these as an array of T, like so:
import scala.collection.mutable.ArrayBuffer
def printArray[a: ArrayBuffer[T]]() = {
// do anything with ArrayBuffer[T]
println(a)
}
Clearly not correct, just passing a single variable case class will work as per below:
def doSomeThing[T]() = {
// ...
}
case class SomeClassA(id: UUID, time: Date)
doSomeThing[SomeClassA]()
But what if I want an ArrayBuffer of a give case class as input?
May be just not possible.
CodePudding user response:
Your doSomeThing
function does not take an argument. It takes a generic argument but no actual values, and with no arguments at all (not even an implicit ClassTag
), there's absolutely nothing you can do with it.
I'll assume you meant
def doSomeThing[T](arg: T) = {
// ...
}
If you want your function to take an arbitrary array buffer instead, you don't replace the thing inside brackets; that's still a free variable. You change the argument type.
def printArray[T](arg: ArrayBuffer[T]) = {
// ...
}