wrong constructor is being picked up

Open
#109 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

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

Research direction

Start with the provided Scala reproduction and the wire[Target] entry point, then compare constructor selection with and without the explicit Target result type. The issue is resolved when the annotated constructor is selected without requiring val sa: Target and the reproduction no longer produces the ambiguity errors.

Written by the indexing model from the issue text.

Description

In below code I am trying to construct Target object. Target class has 3 constructors but only one is preffered (the one annotated). This results in compilation error because wire[Target] picks up wrong constructor. I've intentionally provided to much eligible values here in order to show the naughty behaviour:

  trait A
  trait B
  trait C

  class Target(a: A) {

    def this(b: B) = this(new A{})

    @javax.inject.Inject
    def this(c: C) = this(new A{})
  }

  val a: A = new A {}
  val a1: A = new A {} 
  val b: B = new B {}
  val b1: B = new B {}
  val c = new C {}
  val sa = wire[Target]

Error:(24, 16) Found multiple values of type [com.softwaremill.macwire.macwireakka.Demo.A]: [List(a, a1)]
val sa = wire[Target]
Error:(24, 16) ambiguous reference to overloaded definition,
both constructor Target in class Target of type (c: com.softwaremill.macwire.macwireakka.Demo.C)com.softwaremill.macwire.macwireakka.Demo.Target
and constructor Target in class Target of type (b: com.softwaremill.macwire.macwireakka.Demo.B)com.softwaremill.macwire.macwireakka.Demo.Target
match argument types (Null) and expected result type com.softwaremill.macwire.macwireakka.Demo.Target
val sa = wire[Target]

However if you provide type after sa:

  val sa: Target = wire[Target]

it works fine.

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

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.

More from softwaremill/macwire

All issues in softwaremill/macwire

Similar issues

More Scala issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.