macwire icon indicating copy to clipboard operation
macwire copied to clipboard

Used variable for instantiation differs in Scala 3

Open phkorn opened this issue 2 years ago • 3 comments

Demo Repository

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
}

phkorn avatar Feb 10 '23 16:02 phkorn

@mbore maybe you have an idea if this is possible to fix?

adamw avatar Feb 10 '23 19:02 adamw

If understood macwire as scala implicit(get by Context in this position). It's better to throw a compile time Exception.

djx314 avatar Feb 11 '23 03:02 djx314

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)
}

arteme avatar Nov 04 '24 11:11 arteme