Scala:类型模式匹配期间的前向引用错误

标签 scala compiler-errors forward-reference

我希望了解以下 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 的版本中在 MyClass 内, case class CIn以及常规的 class DIndetermineType 之后定义和声明.尝试匹配 DIn 时生成符号解析错误没有说明符变量。

在脚本范围内,我们同样定义了 case class Cout和一个普通的class DOut .在 determineType 的脚本范围版本中我们有两个不同的错误。第一个是在引用 case class 时生成的。类型 ( Cout ) 没有说明符,它显示“错误的前向引用”。第二个是在引用常规 class 时生成的。类型 ( DOut ) 没有说明符,这是一个符号解析错误。

我是该语言的新手,因此不确定为什么要在 case 中包含说明符语句会影响处理前向引用的方式。

最佳答案

如果你写一个匹配子句case Foo =>您不匹配类型(或类)Foo但是一个值 Foo .所以case Int =>与整数值不匹配,case DIn =>不起作用,因为没有值(或对象)DIn .

以下作品:

val Foo = 1
object Bar

def testVal(x: Any) = x match {
  case Foo => "it's Foo"
  case Bar => "it's Bar"
  case _   => "none of the above"
}

testVal(1)  // Foo!
testVal(Bar) // Bar
testVal(Foo + 1) // none

如果你想匹配一个类的实例,你需要匹配一个子句case b: Baz =>case _: Baz (如果您对将实例绑定(bind)到值不感兴趣)。对于案例类,您可以进一步使用自动提供的提取器,因此使用 case class Baz()您也可以匹配为 case Baz() => .这通常在 Baz 时更有意义。包含特此提取的参数:
case class Baz(i: Int)

def testType(x: Any) = x match {
  case Baz(3) => "Baz with argument 3"
  case b: Baz => "Baz with argument other than 3"
  case Baz    => "Baz companion!"
  case _      => "none of the above"
}

testType(Baz)     // companion object!
testType(Baz(3))
testType(Baz(4))

案例课还给你一个陪伴object所以在这里你实际上可以使用 case Baz => 匹配该对象.一个 object是一种值(value)。

最后,我不知道您使用的是哪个 Scala 版本,但在常规的当前 Scala 中,您不能省略 match determineType 定义的 RHS 上的关键字:
def determineType(x: Any): String = {
  case _ => "anything"
}

<console>:53: error: missing parameter type for expanded function
The argument types of an anonymous function must be fully known. (SLS 8.5)
Expected type was: String
          def determineType(x: Any): String = {
                                              ^


你必须写:
def determineType(x: Any): String = x match /* ! */ {
  case _ => "anything"
}

关于Scala:类型模式匹配期间的前向引用错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33411705/

相关文章:

excel - flink InputStream 类 org.apache.commons.compress.archivers.zip.ZipFile$1 没有实现 InputStreamStatistics

scala - 使用初始化程序 block 进行对象初始化

c++ - 如何正确使用模板?我的模板 <> 存在数据类型问题

Java - 枚举 - 逻辑循环引用

scala - 解释 - 不涉及反射

scala - 查找 Spark 阶段每个步骤的执行时间

c++ - 使用 boost 分离随机数生成器类的接口(interface)和实现

java - 在 Java 中使用 Scala 2.10.1 值类型

c++ - 为什么没有在函数模板的前向引用上选择重载?