How to generate constructor invocation on generic case class? #19363
Unanswered
ChAoSUnItY
asked this question in
Metaprogramming
Replies: 1 comment
-
The constructors expects type arguments first. val baseCtorTerm = Select(New(ownerType), ctor)
val typeAppliedCtorTerm =
if ownerTypeArgs.isEmpty then baseCtorTerm
else TypeApply(baseCtorTerm, ownerTypeArgs.map(t => TypeTree.ref(t.typeSymbol)))
// ^^^^^^^^^^^^^^^^^^^^^^^ here
var ctorTerm = Apply(typeAppliedCtorTerm, paramGens.map(_.asTerm)) // drop the .asExprOf[T], it's not valid Expr yet
var finalTerm = if !ownerTypeArgs.isEmpty then
val genTypeArgs = ownerTypeArgs.map(genTpe => {
genTpe.asType match
case '[g] => getGen[g].asTerm
case _ =>
report.errorAndAbort(
s"Illegal Generator Type: `${Type.show[T]}`, not an valid generator type"
)
})
Apply(ctorTerm, genTypeArgs)
else ctorTerm
finalTerm.asExprOf[T] |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Currently I have a type class that indicates a type is generate-able and having both a non generic and a generic case class to test the constructor invocation:
And here is my attempt to generate constructor invocation through macro:
But after I invokes
fakeGen
on typePerson
andCont[String]
, the first one works without any issue, however, the second one generates an incomplete constructor invocation based on what dotty told me:constructor Cont in class Cont does not take parameters
, which is even more confusing after I dumped 2 constructor invocations throughExpr[T].show
(as shown below):Person
:new Person(given_Generator_String.generate(), given_Generator_Int.generate())
Cont
:new Cont[scala.Predef.String](given_Generator_String.generate(), given_Generator_Int.generate())(given_Generator_String)
Any suggestion about how to fix or complete the incomplete part of constructor invocation would be appreciated!
Beta Was this translation helpful? Give feedback.
All reactions