Scala编译错误:未找到:类型$1

Scala编译错误:未找到:类型$1,scala,pattern-matching,existential-type,scalac,scala-compiler,Scala,Pattern Matching,Existential Type,Scalac,Scala Compiler,我正在研究Scala2.12.x中的存在类型。为此,我正在测试以下代码: trait Parent class ChildA extends Parent class ChildB extends Parent def whatIsInside(opt: Option[_ <: Parent]): String = { opt match { case _: Option[_ <: ChildA] => "ChildA" case _:

我正在研究Scala
2.12.x
中的存在类型。为此,我正在测试以下代码:

trait Parent
class ChildA extends Parent
class ChildB extends Parent

def whatIsInside(opt: Option[_ <: Parent]): String = {
  opt match {
    case _: Option[_ <: ChildA] => "ChildA"
    case _: Option[_ <: ChildB] => "ChildB"
    case _                      => throw new IllegalArgumentException("unknown type")
  }
}


whatIsInside(Some(new ChildA))
trait双亲
类ChildA扩展了父类
类ChildB扩展了父类
def whatisiden(opt:Option[u)抛出新的IllegalArgumentException(“未知类型”)
}
}
里面有什么(一些(新孩子)

由于类型擦除,我不希望它在运行时工作,但它甚至无法编译。我得到以下错误:

[error] ExistentialTypes.scala:12:24: not found: type _$2
[error]         case _: Option[_ <: ChildA] => "ChildA"
[error]                        ^
[error] ExistentialTypes.scala:13:24: not found: type _$3
[error]         case _: Option[_ <: ChildB] => "ChildB"
[error]                        ^
[error]存在类型。scala:12:24:未找到:类型\u2
[错误]大小写:选项[u“ChildA”
[错误]^
[错误]存在类型。scala:13:24:未找到:类型$3
[错误]大小写:选项[u“ChildB”
[错误]^
有人能解释这些错误吗?

(不是完整的答案,而是一些注释和链接;也许可以作为其他人的起点)


在2.12.13中,编译器似乎能够证明
F[1;“由于类型擦除,我不希望这在运行时起作用,但这甚至不能编译。”-这是一种相当奇怪的编译方法。编译器前端的工作就是:在编译时拒绝可能在运行时中断的程序。@Andreytukin这是伪代码。我不想让它起作用,只是想了解为什么你的目标不是让它起作用,或者你的目标真的是研究e语言的局限性吗以前版本的scala编译器中存在的类型?如果是后者,那么可能应该在这个问题上加上“scalac”/“scala编译器”。@Andreytukin确切地说,后者是。我会接受你的建议。谢谢!@MarioGalic我不认为这是“语法”问题。示例中的
F
不引入任何类型变量绑定,而
选项[;非常感谢Andrey的回答。我正在反思你所说的。我对Scala规范的解释不是很流利,但我会让你知道我在想什么,你可以告诉我这对你是否有意义。编译器类型推断能够从
opt
参数计算合成类型,因为它可以提供子类型但是,模式分支中的合成类型必须从其他类型模式而不是实际类型中“计算出来”,因此编译器找不到任何合适的替换,因为它没有“实际的具体输入”,只有另一个模式
println(implicitly[Option[_ <: ChildA] =:= Option[ChildA]])
trait Parent
class ChildA extends Parent
class ChildB extends Parent

def whatIsInside(opt: Option[_ <: Parent]): String = {
  opt match {
    case _: Option[ChildA] => "ChildA"
    case _: Option[ChildB] => "ChildB"
    case None            => "None"
    case _ => throw new Error("meh")
  }
}
trait Parent
class ChildA extends Parent
class ChildB extends Parent

def whatIsInside(opt: Option[_ <: Parent]): String = {
  opt match {
    case _: Option[_ <: ChildA] => "ChildA"
    case _: Option[_ <: ChildB] => "ChildB"
    case None            => "None"
    case _ => throw new Error("meh")
  }
}
def testConforms[A >: Nothing <: ChildA](ca: Option[A]): Option[_ <: Parent] = ca
trait Parent
class ChildA extends Parent
class ChildB extends Parent

def whatIsInside(opt: Option[Parent]): String = {
  opt match {
    case Some(_: ChildA) => "ChildA"
    case Some(_: ChildB) => "ChildB"
    case None            => "None"
    case _ => throw new Error("meh")
  }
}


whatIsInside(Some(new ChildA))
whatIsInside(Some(new ChildB))
whatIsInside(None)