请教elisp中object的写法

下面这段代码:

#+begin_src emacs-lisp :lexical t

#+end_src

#+RESULTS:
: 130

创建过程需要反复调用funcall
(funcall (funcall A 'deposit) 30)
请教下有没有内置的快捷方式写这一段, 比如

(closure-shortcut (A 'deposit) 30)

EIEIO之外, 以上代码有没有其他简便的写法.

那就不要这么上瘾的用函数式编程,对象的属性不好好用struct/object的slot来表达,反而用隐式的闭包捕捉模拟,这回比JS函数式编程还要差了。想要轻量的object用cl-defstruct.

ELISP> (macroexpand '(cl-defstruct fakeobject slot-a slot-b))
(progn
  (defvar cl-struct-fakeobject-tags)
  (cl-defsubst fakeobject-p
      (cl-x)
    (declare
     (side-effect-free error-free))
    (and
     (memq
      (type-of cl-x)
      cl-struct-fakeobject-tags)
     t))
  (put 'fakeobject 'cl-deftype-satisfies 'fakeobject-p)
  (cl-defsubst fakeobject-slot-a
      (cl-x)
    "Access slot \"slot-a\" of `fakeobject' struct CL-X."
    (declare
     (side-effect-free t))
    (progn
      (or
       (fakeobject-p cl-x)
       (signal 'wrong-type-argument
               (list 'fakeobject cl-x)))
      (aref cl-x 1)))
  (cl-defsubst fakeobject-slot-b
      (cl-x)
    "Access slot \"slot-b\" of `fakeobject' struct CL-X."
    (declare
     (side-effect-free t))
    (progn
      (or
       (fakeobject-p cl-x)
       (signal 'wrong-type-argument
               (list 'fakeobject cl-x)))
      (aref cl-x 2)))
  (defalias 'copy-fakeobject #'copy-sequence)
  (cl-defsubst make-fakeobject
      (&cl-defs
       (nil
        (cl-tag-slot)
        (slot-a)
        (slot-b))
       &key slot-a slot-b)
    "Constructor for objects of type `fakeobject'."
    (declare
     (side-effect-free t))
    (record 'fakeobject slot-a slot-b))
  :autoload-end
  (eval-and-compile
    (cl-struct-define 'fakeobject nil 'cl-structure-object 'record nil
                      '((cl-tag-slot)
                        (slot-a)
                        (slot-b))
                      'cl-struct-fakeobject-tags 'fakeobject 't))
  'fakeobject)

另外你不会觉得在defun里用defun就可以像scheme一样把函数定义变成本地的吧。Emacs要定义本地函数只能用cl-flet cl-labels之流

2 个赞

想省略funcall可以(fset A (make-account 100))