开发者

Scala implicit conversion scope issues

Take this code:

  class Register(var value:Int = 0) {
      def getZeroFlag() : Boolean = (value & 0x80) != 0
  }

  object Register {
      implicit def reg2int(r:Register):Int = r.value
      implicit def bool2int(b:Boolean):Int = if (b) 1 else 0
  }

I want to use it like so:

val x = register.getZeroFlag + 10

but I am greeted with:

type mismatch; found : Boolean required: 开发者_运维技巧Int

What goes? Do I need to define a implicit taking a function that returns a bool?


Here's an example demonstrating how to use your implicits:

object Test {
  val register = new Register(42)
  val x = 1 + register // implicitly calling reg2int from companion object
  val y = register - 1 // same
  // val z = register + 1 // doesn't work because "+" means string concatenation

  // need to bring bool2int implicit into scope before it can be used
  import Register._
  val w = register.getZeroFlag() + 2 // this works now
  val z2 = register + 1 // works because in-scope implicits have higher priority 
}

Two potentially non-obvious things here:

  • When seeking implicit conversions to or from an object of type Register, the compiler will look in the companion object Register. This is why we didn't need to bring reg2int explicitly into scope for defining x and y. However, the conversion bool2int does need to be in scope because it's not defined on the Boolean or Int companion object.
  • The method + is already defined on all objects to mean string concatenation via the implicit any2stringadd in scala.Predef. The definition val z is illegal because the implicit for string concatenation takes priority over reg2int (implicits found in companion objects are relatively low priority). However, the definition val z2 works because we've brought reg2int into scope, giving it higher priority.

For more details about how the compiler searches for implicits, see Daniel Sobral's very nice explanation: Where does Scala look for implicits?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜