mirror of
https://github.com/NixOS/nix
synced 2025-06-25 14:51:16 +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:
parent
f4d44a0026
commit
37d7abd694
7 changed files with 61 additions and 27 deletions
|
@ -296,7 +296,7 @@ void checkVarDefs(const ATermMap & defs, Expr e)
|
|||
ATMatcher m;
|
||||
ATerm name;
|
||||
ATermList formals;
|
||||
ATerm body;
|
||||
ATerm with, body;
|
||||
ATermList rbnds, nrbnds;
|
||||
|
||||
if (atMatch(m, e) >> "Var" >> name) {
|
||||
|
@ -340,6 +340,13 @@ void checkVarDefs(const ATermMap & defs, Expr e)
|
|||
}
|
||||
checkVarDefs(defs2, (ATerm) rbnds);
|
||||
}
|
||||
|
||||
else if (atMatch(m, e) >> "With" >> with >> body) {
|
||||
/* We can't check the body without evaluating the definitions
|
||||
(which is an arbitrary expression), so we don't do that
|
||||
here but only when actually evaluating the `with'. */
|
||||
checkVarDefs(defs, with);
|
||||
}
|
||||
|
||||
else if (ATgetType(e) == AT_APPL) {
|
||||
int arity = ATgetArity(ATgetAFun(e));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue