【问题标题】:how do i create a map with a type parameter of class如何创建具有类类型参数的地图
【发布时间】:2010-10-25 08:34:30
【问题描述】:

我正在尝试做这样的事情:

import scala.swing

class componentMapper {

     val map = Map[Class[_], Componenet]()

     def apply(c: Class[_], component: Component) = map += (c -> componenet)

}

class Render extends ComponentMapper {

     def getRenderer(value: AnyRef) = map(value.getClass)

}

这似乎不起作用。我应该为 Class 使用什么类型参数?

【问题讨论】:

  • 我从未使用过 Scala,但我知道它在 JVM 上运行...... Map 不是一个接口而不是一个类吗?改用 HashMap。
  • Scala 有内置地图。上面的 Map[...]() 就像一个静态工厂方法。

标签: scala


【解决方案1】:

我不完全确定核心问题是什么,但这不是类的类型参数。 “->”似乎是一些奇怪的类型推断问题。以下编译和工作正常。

import scala.swing._

class ComponentMapper {
  var map = Map[Class[_], Component]()
  def apply(c: Class[_], component: Component) = map += ((c, component))
}

class Render extends ComponentMapper {
  def getRenderer(value: AnyRef) = map(value.getClass)
}

请注意,我必须对您的代码进行许多小的更正才能找到您所说的问题。

我已经提交了一张罚单,以防万一:https://lampsvn.epfl.ch/trac/scala/ticket/1974

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-16
    • 2020-01-05
    • 1970-01-01
    • 1970-01-01
    • 2016-05-18
    • 2013-08-24
    相关资源
    最近更新 更多