mirror of
https://github.com/NixOS/nix
synced 2025-06-26 15:51:15 +02:00
* Added an experimental feature suggested by Andres: ellipses ("...")
in attribute set pattern matches. This allows defining a function that takes *at least* the listed attributes, while ignoring additional attributes. For instance, {stdenv, fetchurl, fuse, ...}: stdenv.mkDerivation { ... }; defines a function that requires an attribute set that contains the specified attributes but ignores others. The main advantage is that we can then write in all-packages.nix aefs = import ../bla/aefs pkgs; instead of aefs = import ../bla/aefs { inherit stdenv fetchurl fuse; }; This saves a lot of typing (not to mention not having to update all-packages.nix with purely mechanical changes). It saves as much typing as the "args: with args;" style, but has the advantage that the function arguments are properly declared (not implicit in what the body of the "with" uses).
This commit is contained in:
parent
db4f4a8425
commit
9279174dde
14 changed files with 57 additions and 23 deletions
|
@ -84,11 +84,12 @@ static void patternMatch(EvalState & state,
|
|||
ATerm name;
|
||||
ATermList formals;
|
||||
Pattern pat1, pat2;
|
||||
ATermBool ellipsis;
|
||||
|
||||
if (matchVarPat(pat, name))
|
||||
subs.set(name, arg);
|
||||
|
||||
else if (matchAttrsPat(pat, formals)) {
|
||||
else if (matchAttrsPat(pat, formals, ellipsis)) {
|
||||
|
||||
arg = evalExpr(state, arg);
|
||||
|
||||
|
@ -122,8 +123,8 @@ static void patternMatch(EvalState & state,
|
|||
}
|
||||
|
||||
/* Check that each actual argument is listed as a formal
|
||||
argument. */
|
||||
if (attrsUsed != nrAttrs)
|
||||
argument (unless the attribute match specifies a `...'). */
|
||||
if (ellipsis == eFalse && attrsUsed != nrAttrs)
|
||||
throw TypeError(format("the function does not expect an argument named `%1%'")
|
||||
% aterm2String(attrs.begin()->key));
|
||||
}
|
||||
|
@ -402,9 +403,10 @@ Expr autoCallFunction(Expr e, const ATermMap & args)
|
|||
Pattern pat;
|
||||
ATerm body, pos;
|
||||
ATermList formals;
|
||||
|
||||
ATermBool ellipsis;
|
||||
|
||||
/* !!! this should be more general */
|
||||
if (matchFunction(e, pat, body, pos) && matchAttrsPat(pat, formals)) {
|
||||
if (matchFunction(e, pat, body, pos) && matchAttrsPat(pat, formals, ellipsis)) {
|
||||
ATermMap actualArgs(ATgetLength(formals));
|
||||
|
||||
for (ATermIterator i(formals); i; ++i) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue