[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How do I find the names of the fields of a struct?

From: Eduardo Ochs
Subject: How do I find the names of the fields of a struct?
Date: Mon, 7 Jun 2021 21:38:29 -0300

Hi list,

if I define a "mystruct" object with

  (cl-defstruct mystruct fieldA fieldB fieldC)
  (setq mys (make-mystruct :fieldA 22 :fieldC "44"))

and inspect it in the obvious way I get this:

    ;; -> #s(mystruct 22 nil "44")
  (type-of mys)
    ;; -> mystruct
  (recordp mys)
    ;; -> t
  (aref mys 0)
    ;; -> mystruct
  (aref mys 1)
    ;; -> 22
  (aref mys 2)
    ;; -> nil
  (aref mys 3)
    ;; -> "44"

Is there a way to recover the names of the fields of a structure that
has been already defined? I'd like to be able to write a function
`ee-struct-to-string' - meant to be just a very primitive hacking
tool, as simple as possible, with no error checking at all - that
would work like this: this sexp

  (ee-struct-to-string mys)

would return this multi-line string:

  0 type-name-field "mystruct"
  1 fieldA 22
  2 fieldB nil
  3 fieldC "44"

Finding the field name associated to each position in the record is
the tricky part...

  Thanks in advance!
    Eduardo Ochs

reply via email to

[Prev in Thread] Current Thread [Next in Thread]