(功能)在Emacs中有什么用途吗? [英] Does (function) serve any purpose in Emacs?

查看:124
本文介绍了(功能)在Emacs中有什么用途吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

函数表单的文档


像quote一样,但是对于那些是函数的对象来说是首选。
在字节编译中,`function'使其参数被编译。
`quote'不能这样做。


所以一个人会做#'(lambda .. 。)来启用lambda表单的字节编译。



另一方面,如手册中所述,这是不再需要的。


lambda表单有一个其他的效果:它告诉Emacs评估程序和字节编译器,它的参数是一个函数,通过使用 function 作为子例程。

[...]以下表单都是等价的:




 (lambda(x)(* xx))
(function(lambda(x)(* xx)))
# (lambda(x)(* xx))

在这种情况下,这将使函数形式无用。 / p>

有什么其他的情况,函数形式将是有用的?

有没有任何情况下,它的存在既不必要也不等于 quote

解决方案

#' function )由 lambda 内部使用,因为 lambda 被定义为一个包含在函数中的宏。
但除此之外,您确实可以编写几乎任何Elisp代码,而不使用#'。还有另一个微妙的,如果你写了#'foo 你告诉Emacs你认为 foo 是名字的功能,所以最近版本的字节编译器会警告,如果 foo 不是一个已知的函数(就像他们一直在警告关于调用$ code>(foo ...)已经有很多年了)



如@Bruce所指出的,使用#'foo 而不是'foo 也对通过 cl-flet cl-labels :在这种情况下#'foo 是指本地定义的函数,而'foo 将简单地引用 foo 符号,这与实际上与本地函数定义无关,因此可能由未绑定或绑定到另一个函数:

 (cl-flet((a()1))(list'a(functionp' a)#'a))

返回

 (一个零(lambda nil 1)) 


From the documentation of the function form:

Like `quote', but preferred for objects which are functions. In byte compilation, `function' causes its argument to be compiled. `quote' cannot do that.

So one would do #'(lambda ...) to enable byte compilation of the lambda form.

On the other hand, as mentioned in the manual, That is no longer necessary.

The lambda form has one other effect: it tells the Emacs evaluator and byte-compiler that its argument is a function, by using function as a subroutine.
[...] The following forms are all equivalent:

(lambda (x) (* x x)) 
(function (lambda (x) (* x x))) 
#'(lambda (x) (* x x))

This renders the function form useless in this case.

Is there any other situation where the function form would have been useful?
Is there any case when its presence is neither unnecessary nor identical to quote?

解决方案

#' (aka function) is used internally by lambda, since lambda is defined as a macro that wraps itself in a function. But other than that, you indeed can write pretty much any Elisp code without using #'. There is one other subtlety, tho: if you write #'foo you tell Emacs that you think foo is the name of a function, so recent versions of the byte-compiler will then warn if foo is not a known function (just like they've been warning about calls to (foo ...) for many years already).

As pointed out by @Bruce, the use of #'foo instead of 'foo also makes a real difference for functions defined locally via cl-flet or cl-labels: in that case #'foo refers to the locally defined function, whereas 'foo will simply refer to the foo symbol, which is not actually related to the local function definition and may hence by unbound or bound to another function:

(cl-flet ((a () 1)) (list 'a (functionp 'a) #'a))

returns

(a nil (lambda nil 1))

这篇关于(功能)在Emacs中有什么用途吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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