我定义了两个包:(game
文件game.lisp
)和commands
(文件commands.lisp
),由game.asd
文件加载.我在调用命令函数(已导出)时遇到了麻烦,使用(symbol-function (find-symbol "test-function" 'commands))
,返回函数未定义,即使(find-symbol "test-function" 'commands)
返回函数是外部函数,也属于commands
包.
该game.asd
文件的代码是:
(asdf:defsystem "game"
:depends-on (#:cl-ppcre)
:components ((:file "game")
(:file "commands")))
Run Code Online (Sandbox Code Playgroud)
在game.lisp
与启动:
(defpackage :game
(:use :cl :cl-ppcre))
Run Code Online (Sandbox Code Playgroud)
在commands.lisp
与启动:
(defpackage :commands
(:use :cl)
(:export "test-function"))
Run Code Online (Sandbox Code Playgroud)
我需要使用这个in-package
功能吗?从game.lisp
我调用存储在commands.lisp
文件中的命令,其中一些调用一些函数game.lisp
,例如:
(defun test-function ()
(progn
(format *query-io* "Worked!~%")
(start)))
Run Code Online (Sandbox Code Playgroud)
在test-function
位于上的命令包,但调用start
功能,属于game.lisp
.
我打电话时,我希望调用该test-function
函数(symbol-function (find-symbol …
我想从另一个对象内部的指针获取属性的值,但是在不评估引用的情况下访问它会使我出错
When attempting to read the slot's value (slot-value), the slot
POS is missing from the object *NODE-1*.
Run Code Online (Sandbox Code Playgroud)
这是一段模拟错误的代码:
(defclass node ()
((pos
:initarg :pos
:initform '(0 0)
:accessor pos)))
(defclass edge ()
((vertices
:initarg :vertices
:accessor vertices)))
(defparameter *node-1* (make-instance 'node))
(defparameter *node-2* (make-instance 'node :pos '(100 100)))
(defparameter *edge-1* (make-instance 'edge :vertices '(*node-1* *node-2*)))
Run Code Online (Sandbox Code Playgroud)
之后,评估此表达式将引发错误
(slot-value (car (slot-value *edge-1* 'vertices)) 'pos)
Run Code Online (Sandbox Code Playgroud)
但是这个有预期的行为
(slot-value (eval (car (slot-value *edge-1* 'vertices))) 'pos)
Run Code Online (Sandbox Code Playgroud)
我已经知道它eval
可用于丑陋的骇客,这就是为什么我试图找到一种巧妙的方式来做我需要的事情的原因。
我根据用户输入调用函数,但有些函数有两个参数,有些只有一个参数。有没有一种方法可以在参数的值为“NIL”时不传递参数,而不是在每个函数上使用&可选参数(并且从不使用它)?
这是一个交互式小说游戏,其中用户键入一些命令,这些命令将转换为函数调用。
(defun inputs (state)
(format *query-io* "> ")
(force-output *query-io*)
(let* ((entry (cl-ppcre:split "\\s+" (string-downcase (read-line *query-io*))))
(function (car entry))
(args (cdr entry)))
(if (valid-call function)
(funcall (symbol-function (read-from-string function))
state
args)
(progn
(format *query-io* "Sorry, I don't know the command '~a'~%~%" function)
(inputs state)))))
Run Code Online (Sandbox Code Playgroud)
如果用户输入是“装备剑”,我需要调用函数“装备”并传递 '(“剑”) 作为参数,但如果用户输入是“状态”,我需要调用函数“状态”而不需要传递“args”,而不是将它们作为“NIL”传递