Documentation pattern - secondary constructors
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 42/100
- Issue type
- Documentation
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- scala
- Domain
- documentation
Research direction
No documentation file or test is named. Start by locating the macwire documentation entry point for constructors, then incorporate the secondary-constructor, Defaults, and factory patterns shown in the issue. Done means the pattern is documented in the appropriate guide and the examples are understandable in the surrounding documentation.
Written by the indexing model from the issue text.
Description
If you're wondering how to deal with multiple constructors with macwire here is a pattern.
So you have this X class that provides default implementations for some of its parameters:
class X(a: A, b: B, c: C, d: D) {
// secondary constructor, providing defaults for C and D
def this(a: A, b: B) = {
this(a, b, new DefaultC, new DefaultD)
}
}
This can also be written using default parameters (as long as DefaultC or DefaultD does not depend on a or b), but down the line that's the same:
// default parameters, which will be turned into secondary constructors
class X(a: A, b: B, c: C = new DefaultC, d: D = new DefaultD)
(Un)fortunately macwire only looks at the primary constructor (BTW something could be said in the documentation about why this is better).
To workaround that "limitation" here is an alternative:
class X(a: A, b: B, c: C, d: D) {
// no secondary constructor
}
object X {
import Defaults._
def apply(a: A, b: B) : X = wire[X]
trait Defaults {
lazy val c = wire[DefaultC]
lazy val d = wire[DefaultD]
}
object Defaults extends Defaults
}
We can now re-use the defaults provided by X if we're interested in them:
class MyModule extends X.Defaults {
lazy val a: A = wire[AImpl]
lazy val b: B = wire[BImpl]
lazy val x: X = wire[X]
}
Things can get a little more tricky if DefaultC needs some parameters that we're unable to provide. In that case we'll resort to factories:
object X {
import Defaults._
def apply(a: A, b: B, cDep: CDep) : X = {
val c = buildC(cDep)
wire[X]
}
trait Defaults {
def buildC(cDep: CDep) = wire[DefaultC]
lazy val d = wire[DefaultD]
}
object Defaults extends Defaults
}
And in your module
class MyModule extends X.Defaults {
lazy val a: A = wire[AImpl]
lazy val b: B = wire[BImpl]
lazy val cDep: CDep = wire[CDepImpl]
lazy val c: C = buildC(cDep)
lazy val x: X = wire[X]
}
- 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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from softwaremill/macwire
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
softwaremill/macwire#412 · 3 comments ·
-
Difficulty 4/5 3-5 days Newbie friendliness 25/100
softwaremill/macwire#391 · 1 comment ·
-
Difficulty 5/5 Over a week Newbie friendliness 20/100
softwaremill/macwire#301 · 2 comments ·
-
Difficulty 5/5 Over a week Newbie friendliness 20/100
softwaremill/macwire#281 · 5 comments ·
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
softwaremill/macwire#278 · 3 comments ·
All issues in softwaremill/macwire
Similar issues
-
Area: Excel support
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
orbeon/orbeon-forms#7893 ·
-
ShuffleManagerRegistry.register recursion guard is inverted, permits GlutenShuffleManager subclasses Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
Difficulty 1/5 Under an hour Newbie friendliness 70/100