macwire
macwire copied to clipboard
Used variable for instantiation differs in Scala 3
The following code produces a different outcome when using Scala 3:
import com.softwaremill.macwire.wire
case class Berry(var name: String)
case class Basket(berry: Berry)
object Main extends App {
val blackberry: Berry = Berry("blackberry")
val basket: Basket = {
lazy val raspberry: Berry = Berry("raspberry")
wire[Basket]
}
println(basket.berry.name) // scala 2.13.30 raspberry, scala 3.0.2 blackberry
}
@mbore maybe you have an idea if this is possible to fix?
If understood macwire as scala implicit(get by Context in this position). It's better to throw a compile time Exception.
The problem here -in Scala 3- is not that blackberry is chosen over raspberry, but that raspberry is not considered at all!
If blackberry is removed, the code will fail to compile in Scala 3:
object Main extends App {
val basket: Basket = {
lazy val raspberry: Berry = Berry("raspberry")
wire[Basket] // ERROR: Cannot find a value of type: [Berry]
}
println(basket.berry.name)
}