I’m converting an OO codebase to use immutable case classes that will be reconstructed if any modification needs to occur… But there are many OO entities that change frequently, and they are deeply nested, which means I am constantly have to write object.copy(x=value)
. I am fine with this method and don’t want to use an optics library, I would like to just alias the copy
function to be a symbolic operator somehow, let’s say $ so I can keep my LOC terse writing
case class Foo(x: Int, y: Int, z: Int)
val obj = Foo(1,2,3)
val next = obj.$(x=5) // Foo(5,2,3)
Or even better, an infixed operator
val next = obj $ { x=5 } // Foo(5,2,3)
I thought about adding an extension method to all objects, but only case classes have .copy()
. I could extend from Product
which all case classes are, but in that context I cannot assume there is an inherited this.copy
to alias to since not all Products are case classes. Also, I would need the keywordargument notation to work x=value
to implicitly keep all unmodified values the same.
Is there anyway to do this, even by using macros?