There is some support for defclasses as alists in EIEIO but I was wondering if there is any support to read/write a defclass as a property list (plist)?

e.g. using the example

(defclass record () ; No superclasses
  ((name :initarg :name
         :initform ""
         :type string
         :custom string
         :documentation "The name of a person.")
   (birthday :initarg :birthday
             :initform "Jan 1, 1970"
             :custom string
             :type string
             :documentation "The person's birthday.")
   (phone :initarg :phone
          :initform ""
          :documentation "Phone number."))
  "A single record for tracking people I know.")

it would be good to be able to create an instance of this class and then be able to marshall it into a plist (e.g. for over-the-wire transfer)

(:name "Sam" :birthday "Jan 1, 1980" :phone "0123456789")

and be able to do the opposite: take a plist (and the class name 'record) and turn it into an instance of record.

There is some ambiguity around using the slot name, initarg, or accessor name, so I'd suggest just using the initarg for everything to be consistent.

When there is a hierarchy involved, it would be good to include the name of the class in the plist, e.g.

(:type 'record :name "Sam" :birthday "Jan 1, 1980" :phone "0123456789")

I am only really looking for support for Algebraic Data Types: products (simple classes) and coproducts (children of a parent, sealed traits in scala terms).

  • I do not understand what you're looking for. Please give some example. – Stefan May 26 '16 at 1:20
  • OK, I'll write it up in more detail tonight. In short I mean provide a function to convert a defclass instance to a plist, and a function to convert a plist to a defclass, using either the slot or accessor names I don't mind. Happy for it not to support corner cases, I only have simple classes with full accessors. – fommil May 26 '16 at 6:38
  • @Stefan I updated the question, does that make sense? – fommil May 28 '16 at 15:32

EIEIO does come with some support for marshalling/unmarshalling, since that was needed for CEDET (the main drive behind the implementation of EIEIO, AFAIK).

The base function for that is object-write. And it seems that eieio-persistent-convert-list-to-object will convert it back.

This said, EIEIO objects are represented as Elisp vectors which you can print&read like any other Elisp data, so you may not need anything special to do (un)marshalling. Tho admittedly, this is more brittle since this internal representation can change (e.g. it is different in Emacs-25, and it would be different if you change the definition of record).

| improve this answer | |
  • I'm specifically looking for plist representation (it's for ENSIME), although I might be tempted by alist if it is easier (the SWANKY protocol could prefer it over plist). If there is a way to look up the structure of a defclass in a macro I could write something to do this, and also perhaps achieve emacs.stackexchange.com/questions/22544 at the same time? The ability to support inheritance (in the ADT sense) is very important. – fommil May 28 '16 at 21:25
  • And nesting, i.e. a class within a class. If the requirement in that all slots must declare their type, I'd like to think it would be possible to do this. – fommil May 28 '16 at 23:04
  • Have you looked at the output of object-write? What doesn't fit your needs there? – Stefan May 29 '16 at 15:08
  • oh, sorry I thought you meant it wrote out in the internal format! I'll definitely have a look. – fommil May 30 '16 at 9:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.