unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* How do I find the names of the fields of a struct?
@ 2021-06-08  0:38 Eduardo Ochs
  2021-06-08  4:23 ` Eduardo Ochs
  2021-06-08 16:21 ` Stefan Monnier via Users list for the GNU Emacs text editor
  0 siblings, 2 replies; 3+ messages in thread
From: Eduardo Ochs @ 2021-06-08  0:38 UTC (permalink / raw)
  To: help-gnu-emacs

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



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: How do I find the names of the fields of a struct?
  2021-06-08  0:38 How do I find the names of the fields of a struct? Eduardo Ochs
@ 2021-06-08  4:23 ` Eduardo Ochs
  2021-06-08 16:21 ` Stefan Monnier via Users list for the GNU Emacs text editor
  1 sibling, 0 replies; 3+ messages in thread
From: Eduardo Ochs @ 2021-06-08  4:23 UTC (permalink / raw)
  To: help-gnu-emacs

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



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: How do I find the names of the fields of a struct?
  2021-06-08  0:38 How do I find the names of the fields of a struct? Eduardo Ochs
  2021-06-08  4:23 ` Eduardo Ochs
@ 2021-06-08 16:21 ` Stefan Monnier via Users list for the GNU Emacs text editor
  1 sibling, 0 replies; 3+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2021-06-08 16:21 UTC (permalink / raw)
  To: help-gnu-emacs

>   mys
>     ;; -> #s(mystruct 22 nil "44")

Try `cl-prin1` or `cl-prin1-to-string`.

>   (type-of mys)
>     ;; -> mystruct

You can also try `C-h o mystruct RET` (and look at `cl-describe-type`
for how this is implemented).


        Stefan




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-06-08 16:21 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-06-08  0:38 How do I find the names of the fields of a struct? Eduardo Ochs
2021-06-08  4:23 ` Eduardo Ochs
2021-06-08 16:21 ` Stefan Monnier via Users list for the GNU Emacs text editor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).