defaults for function arguments not set in @ case?

27 views
Skip to first unread message

Marc Weber

unread,
Sep 22, 2018, 2:30:30 AM9/22/18
to nix-devel
let pkgs = import <nixpkgs> {};
    dummy = all@{foo, abc ? "abcX"}:  pkgs.lib.attrVals ["foo" "abc"] (pkgs.lib.traceVal(all));
in  dummy {foo = "foo";}


%bin/nix-instantiate --eval-only -strict /tmp/x.nix
trace: { foo = "foo"; }
error: attribute 'abc' missing, at /etc/nixos/nixpkgs/lib/attrsets.nix:84:37

I expect all to have abc set ?

Vladimír Čunát

unread,
Sep 22, 2018, 6:17:59 PM9/22/18
to nix-devel
Hello Marc!
(haven't read about you for a long time)

I believe the semantics in nix has been like this all the time - the "@ pattern" is binding exactly the set that was *passed*.

I think it's perhaps a simpler way than what people would expect intuitively in the most common use cases.  I had been caught by this a long time ago as well, and there's surely a discussion thread about it somewhere (I can't find it fast).  The { foo, bar ? baz } "pattern" doesn't really change what was passed - it just provides bindings to parts of that, with defaults to parts that were missing - at least these are *my* ways to remember since I've found this "issue" long ago, and this semantics actually have become to make much sense to me over time.

I'm not sure if I'm being helpful here.  Anyway, I find this semantics unlikely to change easily, as it would be an incompatible change (though arguably not likely to break many use cases).

--Vladimir

Reply all
Reply to author
Forward
0 new messages