Skip to content

Commit 9c3dd34

Browse files
fricklerhandwerkroberth
authored andcommitted
doc: note that function bindings are accessible in default values
Co-authored-by: Robert Hensing <[email protected]>
1 parent efbd4c1 commit 9c3dd34

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

doc/manual/source/language/syntax.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ three kinds of patterns:
443443
This works on any set that contains at least the three named
444444
attributes.
445445
446-
It is possible to provide *default values* for attributes, in
446+
- It is possible to provide *default values* for attributes, in
447447
which case they are allowed to be missing. A default value is
448448
specified by writing `name ? e`, where *e* is an arbitrary
449449
expression. For example,
@@ -503,6 +503,45 @@ three kinds of patterns:
503503
> [ 23 {} ]
504504
> ```
505505
506+
- All bindings introduced by the function are in scope in the entire function expression; not just in the body.
507+
It can therefore be used in default values.
508+
509+
> **Example**
510+
>
511+
> A parameter (`x`), is used in the default value for another parameter (`y`):
512+
>
513+
> ```nix
514+
> let
515+
> f = { x, y ? [x] }: { inherit y; };
516+
> in
517+
> f { x = 3; }
518+
> ```
519+
>
520+
> This evaluates to:
521+
>
522+
> ```nix
523+
> {
524+
> y = [ 3 ];
525+
> }
526+
> ```
527+
528+
> **Example**
529+
>
530+
> The binding of an `@` pattern, `args`, is used in the default value for a parameter, `x`:
531+
>
532+
> ```nix
533+
> let
534+
> f = args@{ x ? args.a, ... }: x;
535+
> in
536+
> f { a = 1; }
537+
> ```
538+
>
539+
> This evaluates to:
540+
>
541+
> ```nix
542+
> 1
543+
> ```
544+
506545
Note that functions do not have names. If you want to give them a name,
507546
you can bind them to an attribute, e.g.,
508547

0 commit comments

Comments
 (0)