[Nix-dev] Attribute selection with default

Nicolas Pierron nicolas.b.pierron at gmail.com
Thu Sep 24 14:11:55 CEST 2009


On Thu, Sep 24, 2009 at 12:51, Eelco Dolstra <e.dolstra at tudelft.nl> wrote:
> Hi all,
>
> Currently there are many places in the Nixpkgs and NixOS expressions where we
> have to select a possibly missing attribute from a set, e.g.
>
>  attrByPath ["extraModules"] [] mainCfg
>
> or
>
>  getConfig ["python" "db4Support"] true
>
> Writing `attrByPath ["a" "b" "c"] default as' is rather verbose and ugly
> compared to selecting an attribute without a default, i.e. `as.a.b.c'.
>
> Since this is fairly common, how about some special syntax for supplying a
> default value to the "." selection operator?  I propose the optional "or"
> keyword following an attribute selection "x.<attrpath>".  E.g.
>
>  mainCfg.extraModules or []
>
> or
>
>  config.python.db4Support or true
>
> Comments?

The "or" syntax seems a bit too different from the other part of the
language and can be confused with a logical operator from other
languages.
I often use similar patterns where I act on the presence of an attribute:

set ? attr && set.attr <=> attrByPath ["attr"] false set

On the other hand, we are used to have the question mark for default
value inside arguments:

({attr ? default}: attr) set

The problem with this syntax is that that is elements are in reverse
order.  So a possible solution could be to add a similar syntax:

set.{attr ? default}

The next question is how to handle multiple level like `attrByPath
["a" "b" "c"] default as', one possible way is to attribute selections
inside the name of the selected attribute.

as.{a.b.c ? default}

Thus:

set ? attr && set.attr => set.{attr ? false}

What do you think of this proposal?

-- 
Nicolas Pierron
http://www.linkedin.com/in/nicolasbpierron



More information about the nix-dev mailing list