追踪闭包
是否可以在 CL 中追踪闭包?例如,我可以在下面跟踪 foo-3 吗?
(defun foo (n)
(lambda (i) (incf n i)))
FOO
(setf foo-3 (foo 3))
#<CLOSURE :LAMBDA (I) (INCF N I)>
(funcall foo-3 2)
5
(funcall foo-3 2)
7
(trace ???)
Is it possible to trace a closure in CL? For ex., can I trace foo-3 below?
(defun foo (n)
(lambda (i) (incf n i)))
FOO
(setf foo-3 (foo 3))
#<CLOSURE :LAMBDA (I) (INCF N I)>
(funcall foo-3 2)
5
(funcall foo-3 2)
7
(trace ???)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为这是不可能的:据我所知,跟踪宏通常通过将给定符号处的函数替换为调用原始函数并打印出跟踪位的包装器来工作。
如果您对(复杂的)实现细节感兴趣,SBCL 代码位于 src/code/ntrace.lisp (您可能想查看trace-1 函数)。
当然,如果您只想在调用 foo-3 时打印一些内容,那么您始终可以在 foo 的 lambda 形式中放置一条 print 语句...
I don't think this is possible: as far as I know, the trace macro generally works by replacing the function at a given symbol by a wrapper that calls the original and also prints out the tracing bit.
If you're interested in the (complicated) implementation details, the SBCL code is in src/code/ntrace.lisp (you probably want to look at the trace-1 function).
Of course, if all you want to do is print something out when foo-3 is called, you could always put a print statement inside the lambda form in foo...
确实可以这样做。 Trace 在函数命名空间中查找函数,因此请确保不要混合值和函数。
It is indeed possible to do so. Trace looks for functions in the function-namespace, so make sure to not mix values and functions.
我认为这里的问题是
trace
需要函数名称,而不是跟踪闭包有问题。继续上面的示例,您可以从命名函数调用 foo-3 ,并跟踪:I think the problem here is that
trace
requires a function name, rather than there being a problem with tracing closures. Continuing from your example above, you can callfoo-3
from a named function, and trace that: