Nix (language): Difference between revisions

imported>Rovanion
Expanded on the function intro, made the introduction to the more interesting function syntax gradual and understandable.
imported>Rovanion
Corrected the syntax highlighting in the new function sections
Line 99: Line 99:


In nix it happens that sets are given as arguments to functions. Say that we declare a function which returns the concatination of attributes a and b of a set like:
In nix it happens that sets are given as arguments to functions. Say that we declare a function which returns the concatination of attributes a and b of a set like:
<code>
<syntaxHighlight lang=nix>
concat_a_and_b =  set: set.a + set.b  
concat_a_and_b =  set: set.a + set.b  
  concat_a_and_b { a="hello"; b="world"; }
  concat_a_and_b { a="hello"; b="world"; }
"helloworld"
"helloworld"
</code>
</syntaxHighlight>


It is then possible to destructure the argument <code>set</code> and pick out the attributes that we are interested in, in the function declaration, resulting in a tidier function:
It is then possible to destructure the argument <code>set</code> and pick out the attributes that we are interested in, in the function declaration, resulting in a tidier function:


<code>
<syntaxHighlight lang=nix>
  concat_a_and_b = {a, b}: a + b
  concat_a_and_b = {a, b}: a + b
concat_a_and_b { a="hello "; b="world"; }
concat_a_and_b { a="hello "; b="world"; }
"hello world"
"hello world"
</code>
</syntaxHighlight>
==== Default argument ====
==== Default argument ====
It is also possible to assign default values to be used in a function if the caller emits one, but only as part of a set.
It is also possible to assign default values to be used in a function if the caller emits one, but only as part of a set.
<code>
 
<syntaxHighlight lang=nix>
add_a_b = { a ? 1, b ? 2 }: a + b
add_a_b = { a ? 1, b ? 2 }: a + b
add_a_b {}
add_a_b {}
Line 120: Line 121:
add_a_b {a=5;}
add_a_b {a=5;}
7
7
</code>
</syntaxHighlight>


==== Accepting unexpected attributes in argument set ====
==== Accepting unexpected attributes in argument set ====
If you want your function to still run without error if the user provides a set with more attributes than you expected it to have you can use the ellipses.
If you want your function to still run without error if the user provides a set with more attributes than you expected it to have you can use the ellipses.


<code>
<syntaxHighlight lang=nix>
add_a_b = { a, b }: a + b
add_a_b = { a, b }: a + b
add_a_b { a=5; b=2; c=10; }
add_a_b { a=5; b=2; c=10; }
Line 132: Line 133:
add_a_b { a=5; b=2; c=10; }
add_a_b { a=5; b=2; c=10; }
7
7
</code>
</syntaxHighlight>


You can also store away the extra arguments in a name of your chosing using the <code>@</code> pattern.
You can also store away the extra arguments in a name of your chosing using the <code>@</code> pattern.
<code>
 
<syntaxHighlight lang=nix>
add_a_b = rest@{ a, b, ... }: a + b + rest.c
add_a_b = rest@{ a, b, ... }: a + b + rest.c
add_a_b { a=5; b=2; c=10; }
add_a_b { a=5; b=2; c=10; }
17
17
</code>
</syntaxHighlight>