开发者

Why can't call toHexString method from Byte since Scala2.9.0?

Scala2.8.1

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

scala> a.toHexString

res0: String = 1

but 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.toHexS开发者_开发知识库tring
         ^

Why can't call toHexString method from Byte since Scala2.9.0 ?


Scala 2.9.0

If the method toHexString is not defined inside Byte the compiler tries to search for an implicit conversion to a type with the method toHexString but this time it has no luck and that is the reason for the compile error. Actually IMHO RichByte should define a toHexString method (RichInt and RichLong have it).

Scala 2.8.1

I started Scala with scala -Xprint:jvm to see what the compiler has done:

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

As we can see the first the implicit conversion byte2int applies, and after that the implicit conversion intWrapper applies and returns an instance of RichInt where the method toHexString is defined.

But currently I don't know why these two implicit conversions are chained, because actually Scala does not allow chaining of implicit conversions... Anyone can light this up?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜