From 9279174dde3e1a450e63e866d2683352dd8238d3 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Thu, 14 Aug 2008 14:00:44 +0000 Subject: * 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). --- src/libexpr/eval.cc | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'src/libexpr/eval.cc') diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc index ebcbac5391c9..e79d42504d0a 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) { -- cgit 1.4.1