@@ -59,25 +59,25 @@ class LiftableMacros(val c: Context) extends AdtReflection {
59
59
val defNames =
60
60
adts.map(adt => c.freshName(TermName (" lift" + adt.prefix.capitalize.replace(" ." , " " ))))
61
61
val liftAdts = adts.zip(defNames).map { case (adt, defName) =>
62
- val matcher : DefDef = customMatcher(adt, defName, localName).getOrElse( {
62
+ val matcher : DefDef = customMatcher(adt, defName, localName).getOrElse {
63
63
val init = q """ $u.Ident( $u.TermName("_root_")) """ : Tree
64
- val namePath = adt.sym.fullName
65
- .split('.' )
66
- .foldLeft(init)((acc, part) => q " $u.Select( $acc, $u.TermName( $part)) " )
67
- val fields = adt match { case leaf : Leaf => leaf.fields; case _ => Nil }
68
- val args = fields.map(f => {
69
- val fieldName = q " $u.Ident( $u.TermName( ${f.name.toString})) "
70
- val fieldValue =
64
+ def getNamePath (parts : Iterable [String ]): Tree =
65
+ parts.foldLeft(init) { (acc, part) => q " $u.Select( $acc, $u.TermName( $part)) " }
66
+ val nameParts = adt.sym.fullName.split('.' )
67
+ val body = if (adt.sym.isClass) {
68
+ val fields = adt match { case leaf : Leaf => leaf.fields; case _ => Nil }
69
+ val args = fields.map { f =>
71
70
q " _root_.scala.Predef.implicitly[ $u.Liftable[ ${f.tpe}]].apply( $localName. ${f.name}) "
72
71
// NOTE: we can't really use AssignOrNamedArg here, sorry
73
72
// Test.scala:10: warning: type-checking the invocation of method apply checks if the named argument expression 'stats = ...' is a valid assignment
74
73
// in the current scope. The resulting type inference error (see above) can be fixed by providing an explicit type in the local definition for stats.
75
74
// q"$u.AssignOrNamedArg($fieldName, $fieldValue)"
76
- q " $fieldValue"
77
- })
78
- val body = if (adt.sym.isClass) q " $u.Apply( $namePath, $args) " else q " $namePath"
75
+ }
76
+ val namePath = getNamePath(nameParts)
77
+ q """ $u.Apply( $namePath, $args) """
78
+ } else getNamePath(nameParts)
79
79
q " def $defName( $localName: ${adt.tpe}): $u.Tree = $body"
80
- })
80
+ }
81
81
val body : Tree = customWrapper(adt, defName, localName, matcher.rhs).getOrElse(matcher.rhs)
82
82
treeCopy.DefDef (
83
83
matcher,
@@ -89,11 +89,9 @@ class LiftableMacros(val c: Context) extends AdtReflection {
89
89
body
90
90
)
91
91
}
92
- val clauses = adts
93
- .zip(defNames)
94
- .map({ case (adt, name) =>
95
- cq " $localName: ${adt.tpe} => $name( $localName.asInstanceOf[ ${adt.tpe}]) "
96
- })
92
+ val clauses = adts.zip(defNames).map { case (adt, name) =>
93
+ cq " $localName: ${adt.tpe} => $name( $localName.asInstanceOf[ ${adt.tpe}]) "
94
+ }
97
95
q """
98
96
$u.Liftable(( $mainParam: ${weakTypeOf[T ]}) => {
99
97
object $mainModule {
0 commit comments