wvlet / airframe

Essential Building Blocks for Scala
https://wvlet.org/airframe
Apache License 2.0
632 stars 66 forks source link

di (feature): Add scalafmt-friendly DI bind syntaxes #3567

Closed xerial closed 3 months ago

xerial commented 3 months ago

For preserving line breaks in Design bindings, we needed to add this scalafmt config:

optIn.breaksInsideChains = true

This configuration allows aligning bindings each line:

design
  .bind[X].toInstance
  .bind[Y].to[YImpl]

But this configuration can't be used with newlines.source configuration like:

newlines.source = unfold

A new DI binding syntax can be formatted well without using optIn.breaksInsideChains = true:

val design: Design =
  newDesign
    .bindSingleton[A]          // Bind A to a singleton instance of A
    .bindInstance[B](new B(1)) // Bind B to a concrete instance of B
    .bindImpl[A, AImpl]        // Bind A to AImpl
    .bindProvider{(d1: D1) => P(d1) } // Bind P using a provider function
    .bindProvider{(d1: D1, d2: D2) => P(d1, d2) } // Bind P using a provider function
    ...
    .bindProvider{(d1: D1, ..., d5: D5) => P(d1, ..., d5) } // Up to 5 arguments