softwaremill / softwaremill/macwire
Support companion object factory method
@backuitist is already working on this.
Since Sep 8, 2015.
- Dominant language
- Scala
- Stars
- 1.3k
- Forks
- 77
- Avg merge
- 9m
- Merged PRs (30d)
- 4
Description
Sometimes you want to perform some validation before constructing an object. A good place for that is the method factory on the companion object:
class ParamsNeedValidation private(param1: Param1, param2: Param2)
object ParamsNeedValidation {
def apply(param1: Param1, param2: Param2) : ParamsNeedValidation = {
// validate param1 & param2
new ParamsNeedValidation(param1, param2)
}
}
When the primary constructor is found to be private, macwire could give the companion object a chance and IF it has a single apply method, try it.
So we would end up with this:
lazy val pnv = wire[ParamsNeedValidation]
Turned into
lazy val pnv = ParamsNeedValidation(param1, param2)
If the companion object has more than one apply method, a compilation error is returned:
ParamsNeedValidation constructor is private and its companion object has more than one apply method:
- apply(param1, param2)
- apply(param3)
If the companion object has no apply method, then we would fail with the usual:
ParamsNeedValidation constructor is private
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.
Assessment
This issue has not been assessed yet.