布尔值:t vs. nil vs 1 vs -1
在Elisp中,我遇到了用于对布尔值进行建模的不同API.
In Elisp, I've encountered different APIs for modeling boolean values.
我的印象是t
和nil
分别是表示真假的惯用方式.但是,我还看到1
和-1
用于建模相同的事物.
I was under the impression that t
and nil
were the idiomatic ways of representing true and false respectively. However, I've also seen 1
and -1
used to model the same thing.
让我感到困惑的是,我遇到了如果提供nil
则无法使用的API,但是如果使用-1
则可以使用.
What confuses me is that I have come across APIs that won't work if nil
is supplied but will work if -1
is used.
有人可以帮助我了解实际上这是首选的方法.如果答案是t
和nil
,那么我欢迎任何关于为什么某些开发人员将1
和-1
用于其API的理论...
Can someone help me understand which is in fact the preferred way. And if the answer is t
and nil
, I welcome any theories on why some developers use 1
and -1
for their APIs...
sds和sepp2k涵盖了主要的误解,但针对以下问题:
sds and sepp2k have covered the main misconception, but in answer to:
为什么某些开发人员将1和-1用于其API ...
why some developers use 1 and -1 for their APIs...
Emacs次要模式使用正数和负数(或者说是非正数,包括零)来表示启用"和禁用",而不是使用t
和nil
的原因在于是 optional ,并且当未提供可选参数时,其值将为nil
.因此,将无法区分显式传递nil
的自变量和根本不传递自变量.
The reason that Emacs minor modes use positive and negative numbers (or rather non-positive numbers, including zero) to mean "enable" and "disable", rather than using t
and nil
, is that the argument is optional, and when an optional argument is not supplied its value will be nil
. Consequently it would not be possible to distinguish between passing an argument of nil
explicitly, and not passing an argument at all.
从历史上看,不传递任何参数(即nil
的参数)意味着该模式将被切换.
Historically, passing no argument (i.e. an argument of nil
) meant that the mode would be toggled.
这些天,只有在以交互方式调用它时才切换模式,并且参数nil
表示启用".进行此更改是为了保证(add-hook 'prog-mode-hook 'some-minor-mode)
之类的东西-会导致不带任何参数调用some-minor-mode
-保证启用该模式.
These days the mode is only toggled when calling it interactively, and an argument of nil
means "enable". This change was made so that the likes of (add-hook 'prog-mode-hook 'some-minor-mode)
-- which will result in some-minor-mode
being called with no arguments -- is guaranteed to enable that mode.