1

i'm playing around with EIEIO and jeison.el, trying to model a json rest api for an emacs client.

if i define a class, its slots can also specify a :type, and this can be another class i have created. when i instantiate the object, the slot contains another object, also instantiated, of the second class. which is all well and good.

but sometimes the json api also returns an array of objects, and i was hoping to model that similarly, as a list of objects, that will also instantiate like a standalone object in a slot does.

is this possible with EIEIO? it seems like the :type keyword can't handle this, and i'm not sure how else to go about it.

i realise i could just have the objects in the slot could just be a list of alists or similar, but i would like to model the api cleanly, and also some of the objects it returns lists of have to be classes anyway, as they are used elsewhere in the api on their own, i.e. not as slots in, or objects of, other objects. so it seems messy to have them be classes/ojects elsewhere, and then only alists when part of another object.

can decoder or translator functions be used somehow? i'm an oop/eieio newbie, hoping to learn more about it by exploring, and i hit this snag early on. i'd be grateful for any pointers on how to go about this, or where to read up further.

EDIT:

i had a try writing a method that would loop over a list of entries in a given slot and return an object for each entry. i added this to the slot under the :writer keyword, but the EIEIO manual suggests that maybe this isn't implemented:

‘:writer’
     Name of a generic function which will write this slot.

     This option is in the CLOS spec, but is not fully compliant in
     EIEIO.

perhaps what i'm looking to do is simply not possible in EIEIO?

user27075
  • 488
  • 3
  • 11
  • I think your generic list type is the way to go, I wouldn't try to model types precisely. Or do thorough type checking yourself on the accessor method's arguments. – Ehvince May 30 '23 at 14:31
  • @Ehvince if i use `:type list`, then it's still not clear to me how i instantiate each list item as an object of one of my classes? `:type list` on its own just returns the same as if it wasn't specified: a list of alists. – user27075 May 30 '23 at 17:22
  • You instantiate it… in a second time, after the first instantiation? ps: code sample welcome ;) – Ehvince Jun 01 '23 at 09:51

1 Answers1

1

I saw your message on Mastodon. You also say

i was also struggling to have a slot hold an object of the same class as itself (i.e. for mastodon, a boost is a status with a 'reblog' field containing the original status).

I wouldn't try to model this with a slot's type, I think your generic list type is the way to go.

perhaps someone around here knows EIEIO or CLOS or just OOP and has an idea?

I never used eieio

I suggest this Common Lisp CLOS snippet:

CL-USER> (defclass foo-class () 
           ((foo-list :accessor foo-list)))
#<STANDARD-CLASS CL-USER::FOO-CLASS>
CL-USER> (make-instance 'foo-class)
#<FOO-CLASS {1008A029E3}>
CL-USER> (setf (foo-list *) (list 1 2 *))
(1 2 #<FOO-CLASS {1008A029E3}>)
CL-USER> (foo-list **)
(1 2 #<FOO-CLASS {1008A029E3}>)

I create an object of class "foo-class" and I store a list of stuff inside a slot, including a reference to the object, after creation.

To check the types of what we store in foo-list, we could overwrite the accessor method and do the checks ourselves.

(defmethod foo-list (list-of-args)
   (check types here…)
   (setf …))

My 2c… maybe you originally want to specify too much with the types?

Ehvince
  • 1,091
  • 10
  • 14