Let's say you have a Scala function which takes type parameter:
def myFunc[K]: T
Let's say I have several functions like that. Right now, if K could be one of several different values, I'd need some code like the following:
kType match {
case KIsInt => myFunc[Int]
case KIsLong => myFunc[Long]
}
I'd like a generic wrapper function that can switch on kType and call different functions. Is this possible? Something like this.
def kFuncCall[T](kType: KType)(callFunc: [K] => T): T = {
kType match {
case KIsLong => callFunc[Long]
case KIsInt => callFunc[Int]
}
Interesting... I'm doing horrible things with typeclasses and types in https://github.com/gclaramunt/scala-reggen but at the end I resort to casting :(
The first thing it comes to mind is if you know you'll need a typeclass instance you can (again) add it to KType and use it explicitly:
callFunc[kType.K](kType.typeClassOfK)
is not the most elegant way but I think it will work
(maybe is time to mine Scalaz for more sophisticated techniques, :) )