[Nix-dev] Attribute selection with default

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


On Thu, Sep 24, 2009 at 14:22, Eelco Dolstra <e.dolstra at tudelft.nl> wrote:
> Hi,
>
> Nicolas Pierron wrote:
>
>>> 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.
>
> Yes, but it has the advantages of not being an operator (which are never really
> intuitive, unless they're basic mathematical operators) and being very short.
>
>>> I often use similar patterns where I act on the presence of an attribute:
>>>
>>> set ? attr && set.attr <=> attrByPath ["attr"] false set
>
> Yes, that's another common use case.
>
>>> 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}
>
> I don't find this more intuitive than "as.a.b.c or default".
>
> Also, given that the question mark already has two different meanings
> (unfortunately), it shouldn't be used for a third.

Meaning of "?":
- (set ? attr): Check if the set posses an attribute.
- (set@{attr ? default}: ...): Check if the set posses an attribute,
otherwise set it to a default value.
- (set.{attr ? default}) Check if the set posses an attribute and
return its value, otherwise return the default value.

what I see is that the "?" is associated to a check of the presence of
the attribute, and when it is placed inside brackets and after the
attribute this means that you define a default value.

> Same goes for {...}, which is also already taken.  {x ? default} looks too much
> like a function definition.

Indeed, and that's the reason for it.  Keep things familiar and not confusing.

>> in addition to this sugar, you can add dynamic name computation sugar:
>>
>> set.<name>
>
> This is an orthogonal issue (not opposed to it btw), but it would be more
> straightforward to just allow
>
>  set."${expr}"

but ` "${expr}" ' can be rewritten into ` expr ', which could be confusing.


-- 
Nicolas Pierron
http://www.linkedin.com/in/nicolasbpierron
Donald Knuth - I can't go to a restaurant because I keep looking at
the fonts on the menu.



More information about the nix-dev mailing list