Scala implicit conversion scope issues?

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?

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?

Fantastic. That explains the scoping really well. – Dominic Bou-Samra Aug 2 at 3:52.

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?

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions