A while ago (ie. before 0.19) I modified the compiler to allow for a generic expression for the record part of a record update { <expression> | update fields }. The change was rejected based on there being no good case made for that improving the language. I had a very brief search around for code that would be improved because of this change, and I had to admit that I couldn’t really find any “in the wild” code that was “obviously improved” by this change.
My use case is that I have a SPA with many pages, and the model has a generic part Session shared by all these. I have different definitions of getSession in the pages in and general code in Main. In Main, it has to handle all pages to find the session, and inside each page, it is just the same as “.session”. It is a way to be able to program the same in both places, although the context is different.
It is such random limitation, so I am a bit surprised, and I do not see how it improves readability of code.
When I first encountered this limitation, I am mostly an ocaml / F# programmer, I didn’t even understand that the is “by definition”. So, I think one solution would be to improve the compiler error message and clearly say it must be a single variable name between the { and | . The current message says this
I am partway through parsing a record, but I got stuck here:
309| setSession model { getSession model | frms = Session.Requesting "" }
^
I just saw a field name, so I was expecting to see an equals sign next. So try
putting an = sign here?
Note: If you are trying to define a record across multiple lines, I recommend
using this format:
{ name = "Alice"
, age = 42
, height = 1.75
}
While I can see the point for not allowing arbitrary expressions (though I don’t necessarily agree), I wish that at least qualified names were allowed. I.e. right know, you have to write
let
packageRecord = Package.record
in
{ packageRecord | field = newValue }
and I wish we could shorten that to
{ Package.record | field = newValue }
I would argue that in this case the second version is more readable and I think this would be a reasonable tradeoff in syntax complexity vs. usefulness. For example, this would make it a lot more pleasant to provide a record with default options when the user of a package is only expected to change a few of them most of the time.