【问题标题】:Why can't call toHexString method from Byte since Scala2.9.0?为什么 Scala2.9.0 以后不能从 Byte 调用 toHexString 方法?
【发布时间】:2011-06-09 05:20:38
【问题描述】:

Scala2.8.1

scala> val a:Byte = 1
a: Byte = 1

scala> a.toHexString

res0: String = 1

但 Scala2.9.0

scala> val a:Byte = 1
a: Byte = 1

scala> a.toHexString
<console>:9: error: value toHexString is not a member of Byte
       a.toHexString
         ^

为什么Scala2.9.0以后不能从Byte调用toHexString方法?

【问题讨论】:

    标签: scala implicit implicit-conversion


    【解决方案1】:

    Scala 2.9.0

    如果toHexString 方法没有在Byte 中定义,编译器会尝试使用toHexString 方法搜索到类型的隐式转换,但这次它没有运气,这就是编译错误的原因.实际上恕我直言RichByte 应该定义一个toHexString 方法(RichIntRichLong 有它)。

    Scala 2.8.1

    我用scala -Xprint:jvm 启动了Scala,看看编译器做了什么:

    scala> b.toHexString
    
    // ... cutted the unimportant parts
    
    scala.this.Predef.intWrapper(scala.this.Predef.byte2int(line4$object$$iw$$iw.b())).toHexString();
    
    // ... cutted the unimportant parts
    

    我们可以看到第一个隐式转换byte2int 应用,然后隐式转换intWrapper 应用并返回RichInt 的实例,其中定义了方法toHexString

    但目前我不知道为什么这两个隐式转换被链接,因为实际上Scala不允许链接隐式转换......任何人都可以点亮这个?

    【讨论】:

    • 它可能已在 2.9 中归档并修复,这就是它不再起作用的原因。还是我错了?
    猜你喜欢
    • 2013-04-16
    • 1970-01-01
    • 1970-01-01
    • 2015-05-15
    • 1970-01-01
    • 2011-03-02
    • 2015-08-14
    • 2015-07-08
    • 2019-03-18
    相关资源
    最近更新 更多