mirror of
https://github.com/privatevoid-net/nix-super.git
synced 2024-11-22 05:56:15 +02:00
doc: Add example of inherit in a let expression
This commit is contained in:
parent
7ba4e073e8
commit
c819375769
1 changed files with 40 additions and 14 deletions
|
@ -132,6 +132,32 @@ a = src-set.a; b = src-set.b; c = src-set.c;
|
||||||
when used while defining local variables in a let-expression or while
|
when used while defining local variables in a let-expression or while
|
||||||
defining a set.
|
defining a set.
|
||||||
|
|
||||||
|
in a let expression, inherit can be used to selectively bring specific attributes of a set into scope. For example
|
||||||
|
|
||||||
|
|
||||||
|
```nix
|
||||||
|
let
|
||||||
|
x = { a = 1; b = 2; };
|
||||||
|
inherit (builtins) attrNames;
|
||||||
|
in
|
||||||
|
{
|
||||||
|
names = attrNames x;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
is equivalent to
|
||||||
|
|
||||||
|
```nix
|
||||||
|
let
|
||||||
|
x = { a = 1; b = 2; };
|
||||||
|
in
|
||||||
|
{
|
||||||
|
names = builtins.attrNames x;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
both resolve to `{ names = [ "a" "b" ]; }`.
|
||||||
|
|
||||||
## Functions
|
## Functions
|
||||||
|
|
||||||
Functions have the following form:
|
Functions have the following form:
|
||||||
|
@ -146,65 +172,65 @@ three kinds of patterns:
|
||||||
|
|
||||||
- If a pattern is a single identifier, then the function matches any
|
- If a pattern is a single identifier, then the function matches any
|
||||||
argument. Example:
|
argument. Example:
|
||||||
|
|
||||||
```nix
|
```nix
|
||||||
let negate = x: !x;
|
let negate = x: !x;
|
||||||
concat = x: y: x + y;
|
concat = x: y: x + y;
|
||||||
in if negate true then concat "foo" "bar" else ""
|
in if negate true then concat "foo" "bar" else ""
|
||||||
```
|
```
|
||||||
|
|
||||||
Note that `concat` is a function that takes one argument and returns
|
Note that `concat` is a function that takes one argument and returns
|
||||||
a function that takes another argument. This allows partial
|
a function that takes another argument. This allows partial
|
||||||
parameterisation (i.e., only filling some of the arguments of a
|
parameterisation (i.e., only filling some of the arguments of a
|
||||||
function); e.g.,
|
function); e.g.,
|
||||||
|
|
||||||
```nix
|
```nix
|
||||||
map (concat "foo") [ "bar" "bla" "abc" ]
|
map (concat "foo") [ "bar" "bla" "abc" ]
|
||||||
```
|
```
|
||||||
|
|
||||||
evaluates to `[ "foobar" "foobla" "fooabc" ]`.
|
evaluates to `[ "foobar" "foobla" "fooabc" ]`.
|
||||||
|
|
||||||
- A *set pattern* of the form `{ name1, name2, …, nameN }` matches a
|
- A *set pattern* of the form `{ name1, name2, …, nameN }` matches a
|
||||||
set containing the listed attributes, and binds the values of those
|
set containing the listed attributes, and binds the values of those
|
||||||
attributes to variables in the function body. For example, the
|
attributes to variables in the function body. For example, the
|
||||||
function
|
function
|
||||||
|
|
||||||
```nix
|
```nix
|
||||||
{ x, y, z }: z + y + x
|
{ x, y, z }: z + y + x
|
||||||
```
|
```
|
||||||
|
|
||||||
can only be called with a set containing exactly the attributes `x`,
|
can only be called with a set containing exactly the attributes `x`,
|
||||||
`y` and `z`. No other attributes are allowed. If you want to allow
|
`y` and `z`. No other attributes are allowed. If you want to allow
|
||||||
additional arguments, you can use an ellipsis (`...`):
|
additional arguments, you can use an ellipsis (`...`):
|
||||||
|
|
||||||
```nix
|
```nix
|
||||||
{ x, y, z, ... }: z + y + x
|
{ x, y, z, ... }: z + y + x
|
||||||
```
|
```
|
||||||
|
|
||||||
This works on any set that contains at least the three named
|
This works on any set that contains at least the three named
|
||||||
attributes.
|
attributes.
|
||||||
|
|
||||||
It is possible to provide *default values* for attributes, in
|
It is possible to provide *default values* for attributes, in
|
||||||
which case they are allowed to be missing. A default value is
|
which case they are allowed to be missing. A default value is
|
||||||
specified by writing `name ? e`, where *e* is an arbitrary
|
specified by writing `name ? e`, where *e* is an arbitrary
|
||||||
expression. For example,
|
expression. For example,
|
||||||
|
|
||||||
```nix
|
```nix
|
||||||
{ x, y ? "foo", z ? "bar" }: z + y + x
|
{ x, y ? "foo", z ? "bar" }: z + y + x
|
||||||
```
|
```
|
||||||
|
|
||||||
specifies a function that only requires an attribute named `x`, but
|
specifies a function that only requires an attribute named `x`, but
|
||||||
optionally accepts `y` and `z`.
|
optionally accepts `y` and `z`.
|
||||||
|
|
||||||
- An `@`-pattern provides a means of referring to the whole value
|
- An `@`-pattern provides a means of referring to the whole value
|
||||||
being matched:
|
being matched:
|
||||||
|
|
||||||
```nix
|
```nix
|
||||||
args@{ x, y, z, ... }: z + y + x + args.a
|
args@{ x, y, z, ... }: z + y + x + args.a
|
||||||
```
|
```
|
||||||
|
|
||||||
but can also be written as:
|
but can also be written as:
|
||||||
|
|
||||||
```nix
|
```nix
|
||||||
{ x, y, z, ... } @ args: z + y + x + args.a
|
{ x, y, z, ... } @ args: z + y + x + args.a
|
||||||
```
|
```
|
||||||
|
|
Loading…
Reference in a new issue