about summary refs log tree commit diff
path: root/src/libexpr/eval.cc
diff options
context:
space:
mode:
authorEelco Dolstra <e.dolstra@tudelft.nl>2008-08-14T14·00+0000
committerEelco Dolstra <e.dolstra@tudelft.nl>2008-08-14T14·00+0000
commit9279174dde3e1a450e63e866d2683352dd8238d3 (patch)
tree05b5b835f143b97d150a37873641fb5d5e9a7075 /src/libexpr/eval.cc
parentdb4f4a842515392a6b40f5c86b2ef885c1278451 (diff)
* 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).

Diffstat (limited to 'src/libexpr/eval.cc')
-rw-r--r--src/libexpr/eval.cc12
1 files changed, 7 insertions, 5 deletions
diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc
index ebcbac5391..e79d42504d 100644
--- a/src/libexpr/eval.cc
+++ b/src/libexpr/eval.cc
@@ -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) {