[Nix-dev] Attribute selection with default
Nicolas Pierron
nicolas.b.pierron at gmail.com
Thu Sep 24 14:14:34 CEST 2009
On Thu, Sep 24, 2009 at 14:11, Nicolas Pierron
<nicolas.b.pierron at gmail.com> wrote:
> 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
s/that that is/that the/
> 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}
>
in addition to this sugar, you can add dynamic name computation sugar:
set.<name>
which can be combined with the previous operator:
as.{a.<b>.c ? default}
where "name" and "b" are nix expressions.
> What do you think of this proposal?
--
Nicolas Pierron
http://www.linkedin.com/in/nicolasbpierron
Andrew S. Tanenbaum - Never underestimate the bandwith of a wagon full of tapes.
More information about the nix-dev
mailing list