inkytonik / kiama

A Scala library for language processing.
Mozilla Public License 2.0
47 stars 15 forks source link

Avoid reflection in the rewriting dup operation #14

Open inkytonik opened 3 years ago

inkytonik commented 3 years ago

We need a generic way to make a copy of Product value from an example, but without knowing anything statically except that it's a Product. E.g., if we have

abstract class Exp
case class Add(l : Exp, r : Exp) extends Exp
case class Num(i : Int) extends Exp

then we might have Add(1,2) and want to make a copy of this node with new components 3 and 4, so we get an Add(3, 4) instance. If we knew statically that it was an Add (or even maybe Exp would be enough) we could use the copy method. But since we are doing this in a generic traversal, all we know is that it's of some type T <: Product, and copy is not generic.

Currently, we do this via reflection to get the constructor from the known value and call it with the new arguments. This works but ties us to reflection. It would be great to find a way to do this without needing reflection.