softwaremill / softwaremill/macwire

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

Open
#157 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Scala
Stars
1.3k
Forks
77
Avg merge
9m
Merged PRs (30d)
4

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing both examples from the issue and compare the behavior of wire with the provided wireWith workaround. Trace how wire resolves ConcreteObject.Klass and its dependency types; done means the concrete nested types are preserved and both examples compile without the workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
scala
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.