简体   繁体   中英

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.toHexString
         ^

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM