1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-07-07 10:11:47 +02:00

Merge pull request #11672 from fricklerhandwerk/at-pattern-default

doc: note that @-pattern is accessible in default values
This commit is contained in:
John Ericson 2025-03-04 00:57:17 -05:00 committed by GitHub
commit 24463dd025
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -443,7 +443,7 @@ three kinds of patterns:
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,
@ -503,6 +503,45 @@ three kinds of patterns:
> [ 23 {} ] > [ 23 {} ]
> ``` > ```
- All bindings introduced by the function are in scope in the entire function expression; not just in the body.
It can therefore be used in default values.
> **Example**
>
> A parameter (`x`), is used in the default value for another parameter (`y`):
>
> ```nix
> let
> f = { x, y ? [x] }: { inherit y; };
> in
> f { x = 3; }
> ```
>
> This evaluates to:
>
> ```nix
> {
> y = [ 3 ];
> }
> ```
> **Example**
>
> The binding of an `@` pattern, `args`, is used in the default value for a parameter, `x`:
>
> ```nix
> let
> f = args@{ x ? args.a, ... }: x;
> in
> f { a = 1; }
> ```
>
> This evaluates to:
>
> ```nix
> 1
> ```
Note that functions do not have names. If you want to give them a name, Note that functions do not have names. If you want to give them a name,
you can bind them to an attribute, e.g., you can bind them to an attribute, e.g.,