gpt4 book ai didi

scala - "r0"、 "r1"等在 Scala 中有特殊含义吗?

转载 作者:行者123 更新时间:2023-12-04 22:35:26 24 4
gpt4 key购买 nike

如果我将某些 case 对象命名为“r0”、“r1”等,则会出现编译错误。
如果我使用不同的名称,代码将按预期编译和运行。
一个理性的将是非常受欢迎的!

编码:

package qndTests2

sealed abstract case class Reg()
trait RReg
trait RRegNotPc extends RReg
trait LowReg extends RReg
sealed abstract case class LowRegClass() extends Reg with LowReg
sealed abstract case class RRegNotPcClass() extends Reg with RRegNotPc
case object R0 extends LowRegClass
case object R1 extends LowRegClass
case object R2 extends RRegNotPcClass
case object R3 extends Reg with RReg

sealed abstract case class Base()
trait T1
trait T2 extends T1
trait T3 extends T1
sealed abstract case class CaseClassT3() extends Base with T3
sealed abstract case class CaseClassT2() extends Base with T2
case object r0 extends CaseClassT3
case object r1 extends CaseClassT3
case object r2 extends CaseClassT2
case object r3 extends Base with T1

object test {
def regToInt(r: RReg): Int = {
r match{
case R0 => 0
case R1 => 1
case R2 => 2
case R3 => 3
}
}
def toInt(r: T1): Int = {
r match{
case r0 => 0
case r1 => 1 //error: unreachable code
case r2 => 2 //error: unreachable code
case r3 => 3 //error: unreachable code
}
}
def main(args: Array[String]): Unit = {
println(toInt(r0))
println(toInt(r1))
println(regToInt(R0))
println(regToInt(R3))
}
}

“qndTests2”包只包含一个文件,Test.scala,文件的全部内容在上面。
将 "r0"~ "r3"替换为 "A"~ "D",然后编译!
我不明白为什么...
我只是很累,忽略了一些明显的东西???

最佳答案

这不是关于 r0~r3,而是关于小写与大写。看到这个 previous question

您的电话是 language spec 的错误还是功能,但它就在那里,第 8.1.1 节:

A variable pattern x is a simple identifier which starts with a lower case letter. It matches any value, and binds the variable name to that value.

关于scala - "r0"、 "r1"等在 Scala 中有特殊含义吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8067349/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com