Skip to content
This repository was archived by the owner on Feb 19, 2025. It is now read-only.

Commit ed26761

Browse files
Support for multiple parameter lists
1 parent c9d484d commit ed26761

File tree

4 files changed

+45
-7
lines changed

4 files changed

+45
-7
lines changed

pulp/src/main/scala/io/scalaland/pulp/internals/WiredImpl.scala

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,30 @@ private[pulp] class WiredImpl(wiredType: WiredImpl.Type)(val c: Context)(annotte
88
import c.universe._
99

1010
private def buildProviderMethod(classDef: ClassDef): ValOrDefDef = classDef match {
11-
case q"""$_ class $name[..${params: Seq[TypeDef]}] $_(..${ctorParams: Seq[ValDef]})
11+
case q"""$_ class $name[..${params: Seq[TypeDef]}] $_(...${ctorParams: Seq[Seq[ValDef]]})
1212
extends { ..$_ }
1313
with ..$_ { $_ => ..$_ }""" =>
14-
val providerArgs = ctorParams.map(p => q"${p.name}: io.scalaland.pulp.Provider[${p.tpt}]")
14+
val providerArgs = ctorParams.flatten.map(p => q"${p.name}: io.scalaland.pulp.Provider[${p.tpt}]")
1515
val ctorArgs =
16-
if (wiredType != WiredImpl.Type.Singleton) ctorParams.map(p => q"${p.name}.get")
17-
else ctorParams.map(p => q"io.scalaland.pulp.Provider.get[${p.tpt}]")
16+
if (wiredType != WiredImpl.Type.Singleton) ctorParams.map(_.map(p => q"${p.name}.get"))
17+
else ctorParams.map(_.map(p => q"io.scalaland.pulp.Provider.get[${p.tpt}]"))
1818

1919
withTraceLog("Provider implicit expanded") {
2020
wiredType match {
2121
case WiredImpl.Type.Default =>
2222
q"""implicit def implicitProvider[..$params](implicit ..$providerArgs)
2323
: io.scalaland.pulp.Provider[$name[..${params.map(_.name)}]] =
24-
io.scalaland.pulp.Provider.const(new $name[..${params.map(_.name)}](..$ctorArgs))""": DefDef
24+
io.scalaland.pulp.Provider.const(new $name[..${params.map(_.name)}](...$ctorArgs))""": DefDef
2525

2626
case WiredImpl.Type.Factory =>
2727
q"""implicit def implicitProvider[..$params](implicit ..$providerArgs)
2828
: io.scalaland.pulp.Provider[$name[..${params.map(_.name)}]] =
29-
io.scalaland.pulp.Provider.factory(new $name[..${params.map(_.name)}](..$ctorArgs))""": DefDef
29+
io.scalaland.pulp.Provider.factory(new $name[..${params.map(_.name)}](...$ctorArgs))""": DefDef
3030

3131
case WiredImpl.Type.Singleton if params.isEmpty =>
3232
q"""implicit lazy val implicitProvider
3333
: io.scalaland.pulp.Provider[$name[..${params.map(_.name)}]] =
34-
io.scalaland.pulp.Provider.const(new $name[..${params.map(_.name)}](..$ctorArgs))""": ValDef
34+
io.scalaland.pulp.Provider.const(new $name[..${params.map(_.name)}](...$ctorArgs))""": ValDef
3535

3636
case WiredImpl.Type.Singleton if params.nonEmpty =>
3737
c.abort(c.enclosingPosition, "@Singleton cannot be used on parametric types")

pulp/src/test/scala/io/scalaland/pulp/FactorySpec.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,17 @@ class FactorySpec extends FlatSpec {
6464
// then
6565
assertCompiles("Provider.get[ComplexCase[Double]]")
6666
}
67+
68+
it should "generate implicit Provider def for a class with multiple parameter lists" in {
69+
// given
70+
implicit val stringProvider: Provider[String] = Provider.const("test")
71+
implicit val intProvider: Provider[Int] = Provider.const(10)
72+
implicit val doubleProvider: Provider[Double] = Provider.const(20.4)
73+
74+
// when
75+
@Factory class ComplexCase[T](t: T)(name: String)(size: Int)
76+
77+
// then
78+
assertCompiles("Provider.get[ComplexCase[Double]]")
79+
}
6780
}

pulp/src/test/scala/io/scalaland/pulp/SingletonSpec.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,16 @@ class SingletonSpec extends FlatSpec {
2828
assertCompiles("Provider.get[MonoCompanion]")
2929
assertCompiles("Provider.get[MonoCompanion2]")
3030
}
31+
32+
it should "generate implicit Provider def for a class with multiple parameter lists" in {
33+
// given
34+
implicit val stringProvider: Provider[String] = Provider.const("test")
35+
implicit val intProvider: Provider[Int] = Provider.const(10)
36+
37+
// when
38+
@Singleton class ComplexCase(name: String)(size: Int)
39+
40+
// then
41+
assertCompiles("Provider.get[ComplexCase]")
42+
}
3143
}

pulp/src/test/scala/io/scalaland/pulp/WiredSpec.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,17 @@ class WiredSpec extends FlatSpec {
6464
// then
6565
assertCompiles("Provider.get[ComplexCase[Double]]")
6666
}
67+
68+
it should "generate implicit Provider def for a class with multiple parameter lists" in {
69+
// given
70+
implicit val stringProvider: Provider[String] = Provider.const("test")
71+
implicit val intProvider: Provider[Int] = Provider.const(10)
72+
implicit val doubleProvider: Provider[Double] = Provider.const(20.4)
73+
74+
// when
75+
@Wired class ComplexCase[T](t: T)(name: String)(size: Int)
76+
77+
// then
78+
assertCompiles("Provider.get[ComplexCase[Double]]")
79+
}
6780
}

0 commit comments

Comments
 (0)