我尝试编写一些根据输入类型执行不同操作的函数。 例如,我这样做:
def Foo[T](inList: List[String]): ArrayBuffer[T] = {
val out: ArrayBuffer[T] = new ArrayBuffer[T]()
inList.map ( x => {
val str = x.substring(1)
out += str.asInstanceOf[T]
})
out
}
但是如果我用 Foo[Long](new List("123","2342"))
调用这个函数,我会得到一个带有 的
,而不是ArrayBuffer
StringLong
。抱歉我的菜鸟问题,我想了解 scala 和泛型。
最佳答案
- Scala 运行在 JVM 之上,它对泛型一无所知,并且泛型的具体类型在运行时不可用。
因此,您的代码的运行时等效项将如下所示
def Foo(inList: List[String]): (ArrayBuffer[Object]) = {
val out: ArrayBuffer[Object] = new ArrayBuffer[Object]()
inList.map ( x => {
val str=x.substring(1)
out += str.asInstanceOf[Object]
})
(out)
}
- asInstanceOf 不会将字符串转换为 long,而是会抛出有关不兼容类型的异常。相反,您应该为您的函数提供从字符串到另一种类型的转换。
总而言之,您的代码应如下所示:
import scala.collection.mutable.ArrayBuffer
// here we define how convert string to longs
implicit def stringToLong(s: String) = s.toLong
// now this function requires to have converter from string to T in context
def Foo[T](inList: List[String])(implicit f: (String) => T): (ArrayBuffer[T]) = {
val out: ArrayBuffer[T] = new ArrayBuffer[T]()
inList.map { x =>
val str = x.substring(1)
out += f(str) // here we apply converter
}
out
}
// when function is called, appropriate implicit converter from context will be used
Foo[Long](List("51", "42"))
关于Scala 转换为泛型时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26625763/