加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

“r0”,“r1”等在Scala中有特殊意义吗?

发布时间:2020-12-16 18:22:00 所属栏目:安全 来源:网络整理
导读:如果我将一些案例对象命名为“r0”,“r1”等,我会收到编译错误. 如果我使用不同的名称,代码将按预期编译并运行. 这是理性的,非常受欢迎! 代码: package qndTests2sealed abstract case class Reg()trait RRegtrait RRegNotPc extends RRegtrait LowReg ext
如果我将一些案例对象命名为“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.

你的电话是07.0的错误还是功能,但它在那里,第8.1.1节:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读