quil-lang / quilc

The optimizing Quil compiler.
Apache License 2.0
452 stars 73 forks source link

`copy-instance` kwargs to overwrite slots #827

Open gefjon opened 2 years ago

gefjon commented 2 years ago

You can now do e.g. (copy-instance SOME-BINARY-CLASSICAL-INSTRUCTION :left NEW-LEFT-OPERAND) to make a copy of SOME-BINARY-CLASSICAL-INSTRUCTION that has its left slot overwritten with NEW-LEFT-OPERAND. I suspect there are plenty of places in the QuilC code where this would enable a much nicer functional style by eliminating patterns like (let ((copy (copy-instance FOO))) (setf (FOO-BAR FOO) BAR)).

ecpeterson commented 2 years ago

Neat! I tried writing something like this to chase this same pattern, and I was unhappy about having to treat struct and class instances separately, so it never made it in. Some years older and wiser, I think it’s a good enough idea that it should go in anyway.

gefjon commented 2 years ago

I think the only way to do this for subclasses of structure-object is to define individual methods, though probably via a macro. But neither the CL spec nor the MOP defines any way to write a generic method.

Oh, speaking of which, the default method for copy-instance should really specialize on standard-object, not on t. I'll make that change tomorrow and see if it breaks anything.

ETA: let's all take this as a lesson to prefer defclass over defstruct unless profiling suggests a performance incentive otherwise.