Common Lisp中有功能原型吗? [英] Are there function prototypes in Common Lisp?

查看:100
本文介绍了Common Lisp中有功能原型吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用通用Lisp编程了一段时间,在使用Lisp的整个过程中,我还没有看到任何功能/宏的行为都类似于C或C ++中的函数原型.

I have been programming in common lisp for a little while now, and throughout my experience using lisp, I have yet to see any function/macro that acts anything similar to function prototypes in C or C++.

当前,我必须非常注意函数的顺序,否则,当我尝试从另一个函数调用函数时,Lisp说该函数不存在",因为它是在文件的后面定义的.有办法解决这个问题吗?我可以在文件顶部声明所有函数原型,并在下面声明完整的定义吗?

Currently I have to very careful about the ordering of my functions, otherwise, when I try to call a function from another, Lisp says the function "does not exist" because it is defined later in the file. Is there a way to get around this? Can I declare all my function prototypes at the top of the file, and the full definitions below?

推荐答案

声明 声明

您可以使用 声明 全局声明某种事物具有某种功能类型.例如,看看如果定义 foo1 并调用未定义的 baz (在SBCL中),首先会发生什么:

Declaim and Proclaim

You can use declaim to globally declare that a certain thing has a certain function type. For instance, look at what happens first if you define foo1 that calls undefined baz (in SBCL):

CL-USER> (defun foo1 ()
           (baz))

; in: DEFUN FOO1
;     (BAZ)
; 
; caught STYLE-WARNING:
;   undefined function: BAZ
; 
; compilation unit finished
;   Undefined function:
;     BAZ
;   caught 1 STYLE-WARNING condition
FOO1

现在,让我们添加一个声明,说 baz 是不带参数的函数,并返回某些内容.显然,您可以根据需要添加更多的类型信息,但这至少可以提供 baz 是一个函数的信息.

Now, let's add a declamation that says that baz is a function of no arguments, and returns something. You could obviously add more type information if you wanted to, but this will at least provide the arity and the knowledge that baz is a function.

CL-USER> (declaim (ftype (function () t) baz))
; No value

现在,当您定义也调用 baz foo2 时,您将不会收到警告:

Now when you define foo2 that also calls baz, you'll get no warning:

CL-USER> (defun foo2 ()
           (baz))
FOO2

声明是一个宏,但是如果您需要能够在运行时生成其中一些内容,则可以使用

Declaim is a macro, but if you need to be able to generate some of these things at runtime, you can use proclaim, which is a function. E.g.,

CL-USER> (dolist (f '(square cube))
           (proclaim `(ftype (function (number) number) ,f)))
NIL
CL-USER> (defun add-square-and-cube (x y)
           (+ (square x) (cube y)))
ADD-SQUARE-AND-CUBE

也就是说,这不是惯用常见的Lisp.将所需的代码放入文件中,然后编译该文件并加载它,这种情况更为常见.如果由于某种原因无法做到这一点,那将起作用,但是如果有可用的其他加载代码的选项,则值得考虑.

That said, this is not very idiomatic Common Lisp. It's much more common to put the code you need into a file and then to compile that file and load it. If that's not possible for some reason, this will work, but it's worth considering other options of loading your code if they're available.

还值得注意的是,虽然SBCL会从声明中声明或取消声明并静音未定义的函数警告,但实际上该函数仍然是未定义的.其他实现(例如CLISP)仍会发出有关未定义函数的警告.

It's also worth noting that while SBCL will take the hint from proclaim or declaim and silence the undefined function warning, the function is still actually undefined. Other implementations (e.g., CLISP) will still issue a warning about the undefined function.

我真的不建议使用以下方法,因为警告是有原因的,但是您可以选择在评估代码时忽略警告.例如,在CLISP中,当我们使用未定义的函数进行编译时会收到警告:

I don't really recommend the following approach, because warnings are around for a reason, but you can choose to muffle warnings when you evaluate code. E.g., in CLISP, we get a warning when we compile with undefined functions:

CL-USER> (compile nil (lambda () (baz)))
WARNING: Function BAZ is not defined
#<COMPILED-FUNCTION NIL>
1
1

我们可以绑定一个处理程序,该处理程序将使评估表单时发生的所有警告均消失,

We can bind a handler that will muffle any warnings that occur when the form is evaluated, though:

CL-USER> (handler-bind ((warning
                         (lambda (x)
                           (muffle-warning x))))
           (compile nil (lambda () (baz))))
#<COMPILED-FUNCTION NIL>
1
1

这也有其警告,因为编译未定义函数的引用时可能会收到的警告类型可能会有所不同,并且对警告的消声可能也会有所不同.

This has its ow caveats, too, since the type of warning that you might get for compiling a reference to an undefined function might vary, and what muffling the warning does may vary.

这篇关于Common Lisp中有功能原型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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