help-gnu-emacs
[Top][All Lists]
Advanced

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

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


From: Eduardo Ochs
Subject: Re: How do I find the names of the fields of a struct?
Date: Tue, 8 Jun 2021 01:23:33 -0300

On Mon, 7 Jun 2021 at 21:38, Eduardo Ochs <eduardoochs@gmail.com> wrote:
>
> 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:
>
>   mys
>     ;; -> #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
>     http://angg.twu.net/#eev


Problem solved!
The code is here:

  http://angg.twu.net/eev-current/eev-blinks.el

Look for "defun ee-struct-to-string".

  [[]] =),
    http://angg.twu.net/#eev



reply via email to

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