我希望了解以下 Scala 代码生成的编译时错误:
class MyClass {
def determineType(x:Any):String = {
x match {
case Int => "It's an int."
case Float => "It's a Float."
case CIn => "It's a CIn without a specifier."
case c:CIn => "It's a CIn with a specifier."
case DIn=> "It's a DIn without a specifier." // Error:Cannot resolve symbol DIn
case d:DIn=> "It's a DIn with a specifier."
case _ => "It's something else."
}
}
case class CIn()
class DIn
} // End class definition
def determineType(x:Any):String = {
x match {
case Int => "It's an int"
case Float => "It's a Float"
case COut => "It's a COut without a specifier." // Error: Wrong forward reference
case c:COut => "It's a COut with a specifier."
case DOut=> "It's a DOut without a specifier." // Error: Cannot resolve symbol DOut.
case d:DOut=> "It's a DOut with a specifier."
case _ => "It's something else."
}
}
case class COut()
class DOut()
determineType在within的版本中MyClass,thecase class CIn和正则class DIn都在 after 之后定义和声明determineType。尝试匹配DIn没有说明符变量的 a 时会生成符号解析错误。
在脚本范围内,我们同样定义了 acase class Cout和一个正则class DOut。在脚本范围版本中,determineType我们有两个不同的错误。第一个是在引用没有说明符的case class类型 ( Cout) 时生成的,它显示为“错误的前向引用”。第二个是在引用不带说明符的常规class类型 ( DOut) 时生成的,它是符号解析错误。
我是该语言的新手,因此不确定为什么在case语句中包含说明符会影响处理前向引用的方式。