@@ -101,14 +101,33 @@ object QuicklensMacros {
101
101
case _ => Apply (Select (obj, copy), args)
102
102
}
103
103
else if (objSymbol.flags.is(Flags .Sealed ) && objSymbol.flags.is(Flags .Trait )) || objSymbol.flags.is(Flags .Enum ) then
104
- // if the source is a sealed trait / enum, generating a pattern match with a .copy for each child (implementing case class)
104
+ // if the source is a sealed trait / enum, generating a if-then-else with a .copy for each child (implementing case class)
105
105
val cases = obj.tpe.typeSymbol.children.map { child =>
106
106
val subtype = TypeIdent (child)
107
107
val bind = Symbol .newBind(owner, " c" , Flags .EmptyFlags , subtype.tpe)
108
108
CaseDef (Bind (bind, Typed (Ref (bind), subtype)), None , caseClassCopy(owner, mod, Ref (bind), field, tail))
109
109
}
110
- Match (obj, cases)
111
110
111
+ /*
112
+ if (obj.isInstanceOf[Child1]) caseClassCopy(obj.asInstanceOf[Child1]) else
113
+ if (obj.isInstanceOf[Child2]) caseClassCopy(obj.asInstanceOf[Child2]) else
114
+ ...
115
+ else throw new IllegalStateException()
116
+ */
117
+ val ifThens = obj.tpe.typeSymbol.children.map { child =>
118
+ val ifCond = TypeApply (Select .unique(obj, " isInstanceOf" ), List (TypeIdent (child)))
119
+
120
+ val ifThen = ValDef .let(owner, TypeApply (Select .unique(obj, " asInstanceOf" ), List (TypeIdent (child)))) { castToChildVal =>
121
+ caseClassCopy(owner, mod, castToChildVal, field, tail)
122
+ }
123
+
124
+ ifCond -> ifThen
125
+ }
126
+
127
+ val elseThrow = ' {throw new IllegalStateException ()}.asTerm
128
+ ifThens.foldRight(elseThrow) { case ((ifCond, ifThen), ifElse) =>
129
+ If (ifCond, ifThen, ifElse)
130
+ }
112
131
else
113
132
report.throwError(s " Unsupported source object: must be a case class or sealed trait, but got: $objSymbol" )
114
133
0 commit comments