Personally Iām against this feature, in the same way Iām against { a; _ } if you add a parameter that is optional, you want the typer to notify you across the codebase so that you can verify it.
Good point. Then it should probably be opt-in, the same way destructuring is, so e.g. we can construct the record value with {x = 1; _ } to indicate 'I donāt care about the optional fields, just set them to None'.
+1. And if you want to prevent people from constructing t directly, make it private. This way you can safely add new optional fields (well pattern matching can still break but thatās expected).
On the other hand, PPX adds complexity, build time costs, maintenance costs. A small bit of syntax sugar (I see it very similar to letop-punning) would remove that burden from the community.
I never really understood the attraction of letop punning (in the let* x in ... sense). That seemed to me to represent more cognitive load when trying to understand code, shortening the code by a few letters with little practical benefit to be measured against that. On the latest proposal, what is the practical gain in being able to write { x = 1 } instead of { x = 1; y = None }? It seems like another special rule to remember (and check for) when reading and modifying code.
All valid points. Thatās why I think some kind of built-in deriving mechanism is the best answer to this particular problem (granted, it will not be a āsmallā extension, but itās in line with some of the things the dev team are considering).
I agree - I think in the issue/pull request in which it was introduced there was a reference to a specific style of coding/codebase where this kind of simplification would be particularly useful, but in the general case, this seems like unecassary overhead when reading OCaml code.
To me, it feels like the punning, and this syntax proposal are both symptoms of an underlying problem, which is the lack of an easy to use macro system[1], and as such, domain-specific niceties are being pushed into the language (which I guess will probably lead to a C++ - style kitchen-sink scenario in the long term).
[1] - PPXs are useful, but unwieldy to write, even with the helpful ppxlib libraries.
Not much if the record type has just two fields, but potentially a lot more if there are many optional fields, e.g. when dealing with a backward-compatible schema that added many optional fields over time, that should default to None. It would save a ton of keyboarding to say just { x = 1; _ }. Itās a pragmatic consideration.
Punning exists in many parts of OCaml syntax already, and they are super convenient for day-to-day work, so I donāt think my proposal is such a huge deal as to warrant an analysis of underlying problems
To be clear, the issue was not with punning in general, but let-punning as recently introduced, which feels like a more domain specific syntactic sugar.
Maybe it just feels a bit ad-hoc to me because it breaks a lot of mental parsing to have let-bindings without an an associated expression, wheras all the other cases of punning are more coherent with the OCaml syntax.
Maybe so. I guess I just wanted to point out a trend Iāve been seeing; death by a thousand cuts and all that~
Is very close to this when you consider default value annotations.
This also extends beyond the option type. I suspect it will quickly be a friction/annoyance that option types have short hand syntax support and other types do not.
Thanks for the suggestion. This is a nice approach but I think it suffers from two issues, first, as I said earlier it means manual keyboarding to set up the default values, and second, it looks like it would be too easy to forget to override the default non-optional fields. A syntax like a āmakeā function or my suggestion would not have that issue.
This all sounds good until you have a very large code base re-using the same record all across the board. At this point, you learn to appreciate the implicit extensivity that records can bring, for instance adding an extra data point used only in very specific part of your code base without having to needlessly touch millions of other lines of code which, even if it could be done automatically, eventually conflicts with multiple other on-going changes and PRs for no good reason.
This is a thing that I really think should be avoided, in general I really avoid having types crossing the library barrier even in internal libraries, unless those records āareā the API, which means they should not be used internally.
for instance adding an extra data point used only in very specific part of your code base without having to needlessly touch millions of other lines of code which
If the record is being routed across the entirety of the codebase which again it really smells to me, if performance is not needed this should just not happen and if performance is needed then this is gonna be bad anyway as your runtime will need to carry a lot more information.
And if itās not I would say, just duplicate the type and write a mapping function on it on the entry of the library and on the output of it.