1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-29 02:11:15 +02:00

* New language feature: with expressions.

The expression `with E1; E2' evaluates to E2 with all bindings in
  the attribute set E1 substituted.  E.g.,

    with {x = 123;}; x

  evaluates to 123.  That is, the attribute set E1 is in scope in E2.

  This is particularly useful when importing files containing lots
  definitions.  E.g., instead of

    let {
      inherit (import ./foo.nix) a b c d e f;

      body = ... a ... f ...;
    }

  we can now say

    with import ./foo.nix;

    ... a ... f ...

  I.e., we don't have to say what variables should be brought into scope.
This commit is contained in:
Eelco Dolstra 2004-10-25 16:54:56 +00:00
parent f4d44a0026
commit 37d7abd694
7 changed files with 61 additions and 27 deletions

View file

@ -41,11 +41,11 @@ ATerm makePos(YYLTYPE * loc, void * data)
ATermList ts;
}
%type <t> start expr expr_function expr_assert expr_if expr_op
%type <t> start expr expr_function expr_if expr_op
%type <t> expr_app expr_select expr_simple bind inheritsrc formal
%type <ts> binds ids expr_list formals
%token <t> ID INT STR PATH URI
%token IF THEN ELSE ASSERT LET REC INHERIT EQ NEQ AND OR IMPL
%token IF THEN ELSE ASSERT WITH LET REC INHERIT EQ NEQ AND OR IMPL
%nonassoc IMPL
%left OR
@ -67,12 +67,10 @@ expr_function
{ $$ = ATmake("Function(<term>, <term>, <term>)", $2, $5, CUR_POS); }
| ID ':' expr_function
{ $$ = ATmake("Function1(<term>, <term>, <term>)", $1, $3, CUR_POS); }
| expr_assert
;
expr_assert
: ASSERT expr ';' expr_assert
| ASSERT expr ';' expr_function
{ $$ = ATmake("Assert(<term>, <term>, <term>)", $2, $4, CUR_POS); }
| WITH expr ';' expr_function
{ $$ = ATmake("With(<term>, <term>, <term>)", $2, $4, CUR_POS); }
| expr_if
;