“r0”、“r1”是否是“r0”、“r1”?等等在Scala中有特殊含义吗?

发布于 2024-12-14 12:23:28 字数 1510 浏览 2 评论 0原文

如果我将某些 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”,然后编译! 我不知道为什么... 我只是太累了,忽略了一些显而易见的事情吗???

I get a compile error if I name some case objects "r0", "r1" and so on.
If I use different names, the code compile and run as expected.
A rational for that would be very welcome !

the code:

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))
  }
}

the package "qndTests2" contains only one file, Test.scala, and the full content of the file is above.
Replace "r0" ~ "r3" by "A" ~ "D", and it compiles !
I don't the reason why...
am I just very tired, overlooking something obvious ???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

○闲身 2024-12-21 12:23:28

这不是关于r0~r3,而是关于小写与大写。请参阅上一个问题

您的调用无论是错误还是语言规范的功能,但它就在那里,第 8.1.1 节:

变量模式 x 是一个以小写字母开头的简单标识符。它
匹配任何值,并将变量名称绑定到该值。

It's not about r0~r3, it's about lowercase vs uppercase. See this previous question.

Your call whether it's a bug or a feature of the language spec, but it's there, section 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文