airframe icon indicating copy to clipboard operation
airframe copied to clipboard

airframe-di: Failed to resolve a binding of parametric type value

Open choplin opened this issue 6 years ago • 1 comments

When we try to bind a parametric type value within a parametric trait, construction of object fails due to missing dependency.

Suppose we have the following traits;

case class Foo[A](a: A)

trait ParametricService[A] {
  private val alg = bind[Foo[A]]
}

trait ConcreteService {
  private val alg = bind[Foo[Int]]
}

and the following design;

    Design.newDesign
      .bind[Foo[Int]].toInstance(Foo(0))
      .bind[ConcreteService].toSingleton
      .bind[ParametricService[Int]].toSingleton

We can successfully build Foo[Int] and ConcreteService, but the construction of ParametricService[Int] fails due to missing dependency.

I put the complete reproduciable test and log in the gist. https://gist.github.com/choplin/0c1e768914255170535562d4beaae647

choplin avatar Feb 17 '20 15:02 choplin

At the byte code level, ParametricService[A] doesn’t have the information that A is bound to Int, so we need a special treatment to covey the binding A -> Int when instantiating ParametricService[Int]. Basically, we need to extend Airframe to support passing type bindings for each context. For example, A can be String or other types if ParametricService[String] etc. is present.

xerial avatar Feb 17 '20 18:02 xerial