布尔值:t vs. nil vs 1 vs -1 [英] Boolean values: t vs. nil vs 1 vs -1

查看:70
本文介绍了布尔值:t vs. nil vs 1 vs -1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Elisp中,我遇到了用于对布尔值进行建模的不同API.

In Elisp, I've encountered different APIs for modeling boolean values.

我的印象是tnil分别是表示真假的惯用方式.但是,我还看到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.

有人可以帮助我了解实际上这是首选的方法.如果答案是tnil,那么我欢迎任何关于为什么某些开发人员将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次要模式使用正数和负数(或者说是非正数,包括零)来表示启用"和禁用",而不是使用tnil的原因在于是 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.

这篇关于布尔值:t vs. nil vs 1 vs -1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆