Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use primary constructor instead of apply when create case class #637

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,14 @@ object Macros {

def func(t: Type) = {

if (argSyms.length == 0) t
if (argSyms.isEmpty) t
else {
val concrete = tpe.normalize.asInstanceOf[TypeRef].args
if (t.typeSymbol != definitions.RepeatedParamClass) {

t.substituteTypes(typeParams, concrete)
} else {
val TypeRef(pref, sym, args) = typeOf[Seq[Int]]
val TypeRef(pref, sym, _) = typeOf[Seq[Int]]
import compat._
TypeRef(pref, sym, t.asInstanceOf[TypeRef].args)
}
Expand Down Expand Up @@ -301,6 +301,7 @@ object Macros {

val localReaders = for (i <- rawArgs.indices) yield TermName("localReader" + i)
val aggregates = for (i <- rawArgs.indices) yield TermName("aggregated" + i)
val typeTreeOfClass: c.Tree = c.universe.TypeTree(targetType)
q"""
..${
for (i <- rawArgs.indices)
Expand Down Expand Up @@ -355,7 +356,7 @@ object Macros {
}){
this.errorMissingKeys(${rawArgs.length}, ${mappedArgs.toArray})
}
$companion.apply(
new $typeTreeOfClass(
..${
for(i <- rawArgs.indices)
yield
Expand Down
19 changes: 9 additions & 10 deletions upickle/implicits/src-3/upickle/implicits/macros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,8 @@ def tagKeyImpl[T](using Quotes, Type[T])(thisOuter: Expr[upickle.core.Types with
inline def applyConstructor[T](params: Array[Any]): T = ${ applyConstructorImpl[T]('params) }
def applyConstructorImpl[T](using quotes: Quotes, t0: Type[T])(params: Expr[Array[Any]]): Expr[T] =
import quotes.reflect._
def apply(typeApply: Option[List[TypeRepr]]) = {
def apply(appliedTypeOpt: Option[AppliedType]) = {
val tpe = TypeRepr.of[T]
val companion: Symbol = tpe.classSymbol.get.companionModule
val constructorSym = tpe.typeSymbol.primaryConstructor
val constructorParamSymss = constructorSym.paramSymss

Expand All @@ -238,8 +237,8 @@ def applyConstructorImpl[T](using quotes: Quotes, t0: Type[T])(params: Expr[Arra
val lhs = '{$params(${ Expr(i) })}
val tpe0 = constructorTpe.memberType(sym0)

typeApply.map(tps => tpe0.substituteTypes(tparams0, tps)).getOrElse(tpe0) match {
case AnnotatedType(AppliedType(base, Seq(arg)), x)
appliedTypeOpt.map(appliedType => tpe0.substituteTypes(tparams0, appliedType.args)).getOrElse(tpe0) match {
case AnnotatedType(AppliedType(_, Seq(arg)), x)
if x.tpe =:= defn.RepeatedAnnot.typeRef =>
arg.asType match {
case '[t] =>
Expand All @@ -253,18 +252,18 @@ def applyConstructorImpl[T](using quotes: Quotes, t0: Type[T])(params: Expr[Arra
case '[t] => '{ $lhs.asInstanceOf[t] }.asTerm
}
}

}

typeApply match{
case None => Select.overloaded(Ref(companion), "apply", Nil, rhs).asExprOf[T]
case Some(args) =>
Select.overloaded(Ref(companion), "apply", args, rhs).asExprOf[T]
val constructorTree = appliedTypeOpt match {
case Some(AppliedType(tycon, args)) => New(Inferred(tycon)).select(constructorSym).appliedToTypes(args)
case None => New(TypeTree.of[T]).select(constructorSym)
}

constructorTree.appliedToArgs(rhs).asExprOf[T]
}

TypeRepr.of[T] match{
case t: AppliedType => apply(Some(t.args))
case t: AppliedType => apply(Some(t))
case t: TypeRef => apply(None)
case t: TermRef => '{${Ref(t.classSymbol.get.companionModule).asExprOf[Any]}.asInstanceOf[T]}
}
Expand Down
17 changes: 17 additions & 0 deletions upickle/test/src/upickle/MacroTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,23 @@ object TagName{
implicit val quxRw: TagNamePickler.ReadWriter[Qux] = TagNamePickler.macroRW
implicit val fooRw: TagNamePickler.ReadWriter[Foo] = TagNamePickler.macroRW
}


// Defined for compilation test
sealed trait Term

case class Var(name: String) extends Term
object Var {
def apply(name: String): Term = new Var(name)
implicit val rw: RW[Var] = upickle.default.macroRW
}

case class Constant(value: Int) extends Term
object Constant {
def apply(value: Int): Term = new Constant(value)
implicit val rw: RW[Constant] = upickle.default.macroRW
}

object MacroTests extends TestSuite {

// Doesn't work :(
Expand Down
Loading