macwire icon indicating copy to clipboard operation
macwire copied to clipboard

`wire[ConcreteObjectExtendsTrait.ClassDefinedInTrait]` is being treated as `wire[Trait.ClassDefinedInTrait]`

Open richard-shurtz opened this issue 4 years ago • 0 comments

trait ObjectMixIn {
  class Klass()
}

object ConcreteObject extends ObjectMixIn

class App {
  lazy val klass: ConcreteObject.Klass = wire[ConcreteObject.Klass]
}

Fails compilation with

Error:(10, 46) type mismatch;
 found   : ObjectMixIn.this.Klass
 required: ConcreteObject.Klass
  lazy val klass: ConcreteObject.Klass = wire[ConcreteObject.Klass]

The macro is returning the (abstract) ObjectMixIn.this.Klasss type rather than the concrete ConcreteObject.Klass

And

trait ObjectMixIn {
  class Depedency
  class Klass(depedency: Depedency)
}

object ConcreteObject extends ObjectMixIn

class App {
  lazy val depedency: ConcreteObject.Depedency = new ConcreteObject.Depedency
  lazy val klass = wire[ConcreteObject.Klass]
}

Fails compilation with

Error:(12, 24) Cannot find a value of type: [ObjectMixIn.this.Depedency]
  lazy val klass = wire[ConcreteObject.Klass]

So, in this case, it appears that wire is looking for as ObjectMixIn.this.Depedency rather than ConcreteObject.Dependency (and thus isn't finding it)

Described in the general case, it appears that wire[ConcreteObjectExtendsTrait.ClassDefinedInTrait] is being treated as wire[Trait.ClassDefinedInTrait]

My current workaround is to use wireWith, which seems to pull in the full, concrete-type correctly

trait ObjectMixIn {
  class Klass()
  object Klass {
    def apply() = new Klass()
  }
}

object ConcreteObject extends ObjectMixIn

class App {
  lazy val klass: ConcreteObject.Klass = wireWith(ConcreteObject.Klass.apply _)
}

Thankfully, this works without adding too much boilerplate

richard-shurtz avatar Mar 02 '21 04:03 richard-shurtz