【发布时间】:2015-07-03 04:52:53
【问题描述】:
前几天(也许是昨天)我对https://github.com/billstclair/defperson/blob/master/defperson.lisp#L289 中的#+nil 读取时间条件感到非常困惑。
经过深思熟虑,我得出的结论是,这是一种非常糟糕的注释代码方式。有人可以证实这一点吗?
也许我的假设是完全错误的。无论如何,提前谢谢。
【问题讨论】:
标签: common-lisp
前几天(也许是昨天)我对https://github.com/billstclair/defperson/blob/master/defperson.lisp#L289 中的#+nil 读取时间条件感到非常困惑。
经过深思熟虑,我得出的结论是,这是一种非常糟糕的注释代码方式。有人可以证实这一点吗?
也许我的假设是完全错误的。无论如何,提前谢谢。
【问题讨论】:
标签: common-lisp
Yes, it is a lispy way of commenting code,但您不应该在生产代码中忽略它。
更好的选择是#+(or)。
它只需要多一个字符,如果你使用 Emacs paredit 或其他自动插入右括号的模式,它需要相同的按键,并且它不受 *features* 中符号 :nil 的存在的影响。
【讨论】:
#+nil 真的很难搜索。
#+#:ignore。
为了对从输入中读取表达式进行条件化,Common Lisp 使用了 expressions 功能。
在这种情况下,它被用来注释掉一个表单。
它是阅读器的一部分。 #+ 查看下一项(通常作为具有相同名称的关键字符号)是否是列表 *features* 的成员。如果是,则正常读取下一个项目,如果不是,则跳过它。通常:NIL 不是该列表的成员,因此该项目被跳过。因此它对 Lisp 隐藏了表达式。可能有一个 Lisp 实现,但它不起作用:NIL,Lisp 的新实现。它可能在*features* 列表中具有符号:NIL,以指示实现的名称。
NIL 等功能默认在 keyword 包中读取:
#+NIL -> 在cl:*features* 中寻找:NIL
#+CL:NIL -> 在cl:*features* 中寻找CL:NIL
示例
(let ((string1 "#+nil foo bar")) ; string to read from
(print (read-from-string string1)) ; read from the string
(let ((*features* (cons :nil *features*))) ; add :NIL to *features*
(print (read-from-string string1))) ; read from the string
(values)) ; return no values
打印出来:
BAR
FOO
请注意,Common Lisp 有其他方法可以注释掉表单:
; (sin 3) should we use that?
#| (sin 3) should we use that?
(cos 3) or this? |#
【讨论】:
SBCL 忽略了#+nil ... 表达式,即使NIL is 在*features* 中也是如此。如果写入文件,然后加载,则打印“Nope”:(push nil *features*) #+nil (format t "NIL is in *features*") #-nil (format t "Nope").
:NIL 将是 *features* 中的相关符号。
(push :nil *features*) ... 打印出NIL is in *features*。