未在函数定义中使用时,问号在 Nix 中意味着什么?

问题描述

我知道 a similar question,但它指的是 function definitions with a set pattern (pinned) 中使用的 ?

我查找了 lib.makeOverridable 的实现并发现了 this line

  ${if result ? overrideAttrs then "overrideAttrs" else null} = fdrv:

(还了解到这种动态分配是可能的,所以这是一个奖励。)

解决方法

根据 15.3. Operatorspinned (Nix manual) 的表格:

表 15.1。运营商

| Name          | Syntax       | Description                     |
+---------------+--------------+---------------------------------+
|               |              | Test whether set e contains the |
| Has Attribute | e ? attrpath | attribute denoted by attrpath;  |
|               |              | return true or false.           |
+---------------+--------------+---------------------------------+

示例:

$ nix repl
Welcome to Nix version 2.3.10. Type :? for help.

nix-repl> set = { a = 27; b = "lofa"; }

nix-repl> set ? a
true

nix-repl> set ? lofa
false

nix-repl> supset = { c = set; d = 7; }

nix-repl> supset ? c.a
true