Scala 转换为泛型时遇到问题

标签 scala generics casting

我尝试编写一些根据输入类型执行不同操作的函数。 例如,我这样做:

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 String,而不是Long。抱歉我的菜鸟问题,我想了解 scala 和泛型。

最佳答案

  1. 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/

    相关文章:

    ios - Swift - 如何用不同类型的枚举声明变量/函数?

    casting - 在 Go 中对多个返回值进行转换/类型断言的惯用方法

    c - 为什么只有 1% 的时间我会得到不同的结果?

    c# - 通过 cast 或 Convert.ToSingle() 将 double 转换为 float?

    xml - 对象 xml 不是包 scala 的成员

    java - 为什么类型参数绑定(bind) T < : Comparable[T] fail for T = Int?

    java - Scala/Java 枚举

    java - 根据输入进行切换并返回动态值

    c# - 在 Autofac 中使用接口(interface)作为类型参数解析泛型

    scala - 1::List[Nothing] in foldLeft