Withers as procedural macros
Brian Goetz
brian.goetz at oracle.com
Sat Nov 30 20:46:51 UTC 2024
For completeness, I would like to point out a similarity between the reconstruction block of a wither expression and the compact constructor of a record. Both have a set of synthetic mutable locals, corresponding to the record components, and operate by mutating those locals. The difference is how the locals get their values (parameters, or getters) and what they do with them (chain to a constructor, or invoke a constructor).
On Nov 30, 2024, at 9:42 AM, Aaryn Tonita <atonita at proton.me> wrote:
I am very excited about withers, immutable objects are really inconvenient without such utility methods. I recently saw the distinction in the thread on '"With" for records and the default constructor' between withers and nominal constructors. I too have wished for such nominal constructors, and took notice that it isn't a priority. Fair enough.
But that got me thinking about why I wanted it and how else it could be done in user land and I recalled that JEP 468 makes an excellent point for the flexibility of use site creation of new values to avoid bloat on the declaring class. This explanation feels very much like the power of procedural macros which can generate the obvious boilerplate binding at the use site. If you were in fact adding procedural macro support (through something roughly equivalent to annotation processors) then the community could fill in the gaps for nominal creation and many other features. In either case withers seem to be work that the compiler has to perform at the use site and that can either be built in or plugged in.
In another thread you pointed out that you see this as a regularization feature and not a cool new feature which procedural macros certainly would be. However, the current syntax seems like it would be hard to retro fit to a procedural macro later on (if you would decide that later) while a different syntax might allow you to retrofit the work the compiler must do as a library procedural macro.
I figure you must have considered procedural macros at some point. Certainly java has some code generation functionality as is but to me it does feel quite different than procedural macros (or the higher order function capabilities of python annotations) feel in other languages. Annotation processors feel like a genuinely separate build step. In fact, powerful string templating could be delivered with such a feature where the procedural could read format!("{foo}") and close over the scoped variable foo or fail to compile when it is not in scope. So something like with!(point, x=localX, y=localY) could yield this and more like with!(point3D, point2D, z=localZ) for a nominal construction?.
That syntax definitely feels very un-java though.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20241130/5d3ca4d0/attachment.htm>
More information about the amber-dev
mailing list