【问题标题】:scala - can't existentially abstract over parameterized typescala - 不能在参数化类型上存在抽象
【发布时间】:2016-04-02 11:59:18
【问题描述】:

在这个简单的例子中,我创建了一个基于泛型类型的容器类:

class  Student[T](val favoriteThing:T, var partner:Option[Student[T]]=None )

第一个字段favoriteThing 是泛型类型T,第二个字段是指向该类的另一个实例的指针,但属于Option 类型,它的默认值为None

我可以实例化这个类并检查第一个字段

val s1 = new Student(42)
s1.favoriteThing

但在访问 partner 字段时会出现此运行时错误。

s1.partner
Compiler exception error: line 0: can't existentially abstract over parameterized type Student[Int]
  def apply() = {
            ^

感谢您对理解此错误的任何帮助。

【问题讨论】:

  • 似乎缺少一些东西,因为上面的代码对我来说运行顺利。您使用的是什么 scala 版本?
  • 你可能在 block 中有一个未绑定的 Type Constructor。喜欢{class A[B] {}; new A[Int]}。欲了解更多信息:stackoverflow.com/questions/3122398/…
  • 请发布更多相关代码,您发布的内容正常。
  • @SaschaKolberg 我的行为相同。它只是工作。
  • 感谢您的反馈 - 我一直在用 scalafiddle scalafiddle.net/console/e25f42d74afdf8c6ca6a3b7ece3af051 进行测试,这似乎是问题的根源。以后对scalafiddle会更加谨慎...

标签: scala


【解决方案1】:

这不是 scala 的问题。问题在于使用 scalafiddle

http://scalafiddle.net/console/e25f42d74afdf8c6ca6a3b7ece3af051

这会在该上下文之外正确编译和运行。

【讨论】:

    猜你喜欢
    • 2011-03-08
    • 1970-01-01
    • 1970-01-01
    • 2018-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多