Why is merging commutative? #838
-
Having used Jsonnet for a bit, I was quite surprised to learn that in Nickel, one can't do something like { networking.open_ports = [ 123 ] } & { networking.open_ports = [ 456 ] } expecting the result to be I assume this is because the Why does Nickel not behave this way? Why is symmetry important? Why can't the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Symmetry was taken from both CUE and the NixOS module system approach. The motivation in CUE is discussed here for example, and in other places of the documentation. For NixOS, the user is not the one doing the merge, but it's the NixOS module library. In that context, you don't control the order of merging, it is unspecified, so you need to rely on something else to control which value takes precedence over the other. But your case has actually two aspects. if However, as you say, merging other things that records is still very valuable in practice. This is why RFC001 proposes to let the user specify custom merge functions. You could then write your example as:
(or something like that, leaving a few details out). Custom merge functions ought to be commutative too, but this notion has to be nuanced. Here, we use an array for open ports, but in fact the order is not important: actually, the right data structure we are thinking of is an unordered set, not an array. But arrays are a good enough representation. In that situation, while Sorry for the long post, so let's recap:
RFC001 has been written some time ago, and hasn't been a priority, but we started implementing it recently (see e.g. #829). I think custom merge functions are not too far in the pipeline 🙂 |
Beta Was this translation helpful? Give feedback.
Symmetry was taken from both CUE and the NixOS module system approach. The motivation in CUE is discussed here for example, and in other places of the documentation. For NixOS, the user is not the one doing the merge, but it's the NixOS module library. In that context, you don't control the order of merging, it is unspecified, so you need to rely on something else to control which value takes precedence over the other.
But your case has actually two aspects. if
&
doesn't act on arrays as@
, it is first and foremost because there seem to be no universal and natural definition of merging on arrays. You could also decide that[x, y] & [a,b]
is[x & a, y & b]
for example (that would actually …