Nix Language Quirks: Difference between revisions

imported>Danbst
added old let syntax
imported>Danbst
added function argument destructuring
Line 31: Line 31:


Note, that it isn't equivalent to <code>with rec { x = 1; y = x + 1; body = y; }; body</code> because of mentioned <code>with</code> and <code>let</code> quirk, but is same as <code>rec { x = 1; y = x + 1; body = y; }.body</code>
Note, that it isn't equivalent to <code>with rec { x = 1; y = x + 1; body = y; }; body</code> because of mentioned <code>with</code> and <code>let</code> quirk, but is same as <code>rec { x = 1; y = x + 1; body = y; }.body</code>
== Something that looks like both record attribute and <code>let</code>-binding ==
Destructuring function argument - is a great feature of Nix.
<nowiki>
nix-repl> f = { x ? 1, y ? 2 }: x + y
nix-repl> f { }
3</nowiki>
The fact that we can add <code>@args</code> argument assignment is also cool
<nowiki>
nix-repl> f = { x ? 1, y ? 2, ... }@args: with args; x + y + z
nix-repl> f { z = 3; }
6</nowiki>
But don't be fooled, <code>args</code> doesn't necessarily contain <code>x</code> and <code>y</code>:
<nowiki>
nix-repl> f = { x ? 1, y ? 2, ... }@args: args.x + args.y + args.z
nix-repl> f { z = 3;}
error: attribute ‘x’ missing, at (string):1:30</nowiki>
These <code>x</code> and <code>y</code> are in fact <code>let</code>-bindings, but overridable ones.