如何实现 `cl-print-object`?

huangapple go评论52阅读模式
英文:

How to implement `cl-print-object`?

问题

我正在使用''eieio' 和''cl-lib',并尝试按照此处描述的方法为我的类定义'object-print'方法:

https://www.gnu.org/software/emacs/manual/html_mono/eieio.html#Basic-Methods

  "返回一个包含数据对象摘要的字符串作为名称的一部分。"
  (apply #'cl-call-next-method this
         (format " 值: %s" (render this))
         strings))

我想这样做是为了在使用 edebug 时看到对象实例的内容。

当我在自己的代码中实现这个示例时,它会产生错误消息“函数‘render’未被定义”。

除了错误消息之外,Flycheck建议:“‘object-print’是过时的泛型函数(自26.1起);改用‘cl-print-object’。”我在谷歌上找不到关于‘cl-print-object’函数的任何文档,并且我无法猜出如何实现它:

  "返回一个包含数据对象摘要的字符串作为名称的一部分。"
  (apply #'cl-call-next-method this
         (format " 值: %s" (slot-value this 'value))
         strings))

我不知道要提供什么来代替‘(slot-value this 'value)’。它似乎不想要一个槽。

我如何实现‘cl-print-object’函数?或者在 edebug 内部进行调试时,有没有更好的方法来查看 EIEIO 对象实例的内容?

英文:

I am using 'eieio and 'cl-lib and I'm trying to define the object-print method for my class as described here:

https://www.gnu.org/software/emacs/manual/html_mono/eieio.html#Basic-Methods

(cl-defmethod object-print ((this data-object) &optional strings)
  "Return a string with a summary of the data object as part of the name."
  (apply #'cl-call-next-method this
         (format " value: %s" (render this))
         strings))

I want to do this so I can see the contents of my object instances when I'm using edebug.

When I implement this example in my own code it produces the error message the function ‘render’ is not known to be defined.

In addition to the error message, Flycheck suggests: ‘object-print’ is an obsolete generic function (as of 26.1); use ‘cl-print-object’ instead. I cannot find any documentation on the cl-print-object function when I Google, and I am failing at guessing how to implement it:

(cl-defmethod cl-print-object ((this card) &optional strings)
  "Return a string with a summary of the data object as part of the name."
  (apply #'cl-call-next-method this
         (format " value: %s" (slot-value this 'value))
         strings))

I do not know what to provide instead of (slot-value this 'value). It does not seem to want a slot.

How can I implement the cl-print-object function? Or is there a better way to see the contents of an EIEIO object instance when debugging inside edebug?

答案1

得分: 2

我不理解elisp的CLOSoid系统,但类似这样的东西至少是一个开始:

(defclass foo ()
  ((x :initform 1
      :inirarg :x
      :reader foo-x)))

(defmethod cl-print-object ((f foo) stream)
  (princ (format "#<%s x %s>"
                 (class-name (class-of f))
                 (foo-x f))
         stream))

现在:

ELISP> (make-instance 'foo :x 2)
#<foo x 2>
ELISP> (defclass bar (foo) ())
bar
ELISP> (make-instance 'bar :x 2)
#<bar x 2>

显然在实际应用中,你希望从对象中提取更多信息:我不知道是否有CL的print-unreadable-object的等价物,它提供了一种不需要手动处理所有工作的好方法。

英文:

I don't understand elisp's CLOSoid system, but something like this is at least a start:

(defclass foo ()
  ((x :initform 1
      :inirarg :x
      :reader foo-x)))

(defmethod cl-print-object ((f foo) stream)
  (princ (format &quot;#&lt;%s x %s&gt;&quot;
                 (class-name (class-of f))
                 (foo-x f))
         stream))

Now:

ELISP&gt; (make-instance &#39;foo :x 2)
#&lt;foo x 2&gt;
ELISP&gt; (defclass bar (foo) ())
bar
ELISP&gt; (make-instance &#39;bar :x 2)
#&lt;bar x 2&gt;

Obviously in real life you want to extract more information from the object: I don't know if there is an equivalent of CL's print-unreadable-object which provides a nice approach to this without requiring you do to all the work by hand.

huangapple
  • 本文由 发表于 2023年4月16日 23:48:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76028765.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定