* EIEIO accessing class slots
@ 2021-06-10 15:20 pillule
2021-06-10 19:38 ` pillule
2021-06-10 22:02 ` Stefan Monnier via Users list for the GNU Emacs text editor
0 siblings, 2 replies; 14+ messages in thread
From: pillule @ 2021-06-10 15:20 UTC (permalink / raw)
To: help-gnu-emacs
Hi
How can I accede to the initarg of an eieio class ?
Let's say I have a class as :
(defclass moirai-dired-side ()
((display-alist
:initarg :display-alist
:initform '((moirai-dired-p)
(display-buffer--in-dired-side-window)
(slot . 0) (side . left) (select . t)
(preserve-size (t . t)) (window-width . 35)
(window-parameters . ((no-other-window . t)
(no-delete-other-windows . t))))
:custom (custom-variable-type 'display-buffer-alist)
:documentation
"Alist of conditionals actions for `display-buffer',
It is the same format that `display-buffer-alist'.")))
Now I can create instances with
(setf morai-dired-side-left (moirai-dired-side))
And so far manipulating the object will work fine,
but if we go back to the class itself ...
;; apparently this is not an eieio-class ?!
(eieio--class-p 'morai-dired-side)
;; => nil
;; but I can anyway access its slots
(eieio-class-slots 'moirai-dired-side)
;; => (#s(cl-slot-descriptor display-alist '((moirai-dired-p) (display-buffer--in-dired-side-window) (slot . 0) (side . left) (select . t) (preserve-size (t . t)) (window-width . 35) (window-parameters (no-other-window . t) (no-delete-other-windows . t))) t ((:documentation . "Alist of conditionals actions for `display-buffer',
It is the same format that `display-buffer-alist'.") (:custom custom-variable-type 'display-buffer-alist) (:group default))))
;; the issue is I have no idea of how to get them individually
(eieio--class-slot-initarg 'moirai-dired-side :display-alist)
;; ! Wrong type argument: eieio--class, moirai-dired-side, class
Am I missing something ? I seen a bug report from 2015 at this subject (#bug19552) but it doesn't help.
As a side note the eieio manual is outdated, it references 'class-slot-initarg',
while it is now 'eieio--class-slot-initarg'.
--
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: EIEIO accessing class slots
2021-06-10 15:20 EIEIO accessing class slots pillule
@ 2021-06-10 19:38 ` pillule
2021-06-10 22:03 ` Stefan Monnier via Users list for the GNU Emacs text editor
2021-06-10 22:02 ` Stefan Monnier via Users list for the GNU Emacs text editor
1 sibling, 1 reply; 14+ messages in thread
From: pillule @ 2021-06-10 19:38 UTC (permalink / raw)
To: pillule; +Cc: help-gnu-emacs
pillule <pillule@riseup.net> writes:
> Hi
>
> How can I accede to the initarg of an eieio class ?
I finally found that I can access a class-slot-initarg with 'oref-default', which apparently works on both objects and class.
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: EIEIO accessing class slots
2021-06-10 15:20 EIEIO accessing class slots pillule
2021-06-10 19:38 ` pillule
@ 2021-06-10 22:02 ` Stefan Monnier via Users list for the GNU Emacs text editor
2021-06-10 23:04 ` pillule
1 sibling, 1 reply; 14+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2021-06-10 22:02 UTC (permalink / raw)
To: help-gnu-emacs
> How can I accede to the initarg of an eieio class?
What do you mean exactly by that and why do you need that?
> ;; apparently this is not an eieio-class ?!
> (eieio--class-p 'morai-dired-side)
> ;; => nil
`eieio--class-p` checks if the arg is a class object, where you passed
a symbol which is the *name* of the class object.
> As a side note the eieio manual is outdated, it references 'class-slot-initarg',
> while it is now 'eieio--class-slot-initarg'.
Thanks, I'll try and see how to adjust it,
Stefan
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: EIEIO accessing class slots
2021-06-10 19:38 ` pillule
@ 2021-06-10 22:03 ` Stefan Monnier via Users list for the GNU Emacs text editor
0 siblings, 0 replies; 14+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2021-06-10 22:03 UTC (permalink / raw)
To: help-gnu-emacs
> I finally found that I can access a class-slot-initarg with 'oref-default',
> which apparently works on both objects and class.
What do you mean by "access a class-slot-initarg"?
Stefn
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: EIEIO accessing class slots
2021-06-10 22:02 ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2021-06-10 23:04 ` pillule
2021-06-11 0:04 ` Stefan Monnier via Users list for the GNU Emacs text editor
0 siblings, 1 reply; 14+ messages in thread
From: pillule @ 2021-06-10 23:04 UTC (permalink / raw)
To: Stefan Monnier; +Cc: help-gnu-emacs
Stefan Monnier via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org> writes:
>> How can I accede to the initarg of an eieio class?
>
> What do you mean exactly by that and why do you need that?
>
I have a class 'moirai-dired-side' ; I make instances from it that I store as frame parameters, eg, 'moirai-dired-side-left' or 'moirai-dired-side-right'.
The following function is a function usable by 'display-buffer'.
At the moment of its call, an object of the class may already exist, or not.
Depending from which window it is called, I want to adjust the get-or-create of the object.
I could use 'left as I know it is the default value, but this is cutting grass under foots for future customizations.
This need adjustments but I hope you get the idea.
(defun display-buffer-in-dired-side-window (buffer alist)
"Display BUFFER in a side window with `dired-side-window-parameters' as ALIST.
The side parameter that may be inherited from a selected dired side window.
Only apply for buffers using `dired-mode' without `inhibit-same-window'."
(when (and (dired-buffer-p buffer)
(not (cdr (assq 'inhibit-same-window alist))))
(let* ((side (or (window-parameter (selected-window) 'window-side)
(alist-get 'side (oref-default 'moirai-dired-side :display-alist))))
(strand-name (intern (concat (prin1-to-string 'moirai-dired-side)
"-"
(prin1-to-string side))))
(strand (morai-get-or-create 'moirai-dired-side strand-name)))
(let ((window (display-buffer-in-side-window buffer (cdr (oref strand :display-alist)))))
(oset strand :buffer buffer)
(oset strand :window window)
(morai-update-prev-buffers strand window)
window))))
>> ;; apparently this is not an eieio-class ?!
>> (eieio--class-p 'morai-dired-side)
>> ;; => nil
>
> `eieio--class-p` checks if the arg is a class object, where you passed
> a symbol which is the *name* of the class object.
Then how to access to the object ?
--
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: EIEIO accessing class slots
2021-06-10 23:04 ` pillule
@ 2021-06-11 0:04 ` Stefan Monnier via Users list for the GNU Emacs text editor
2021-06-11 0:23 ` pillule
2021-06-11 2:52 ` pillule
0 siblings, 2 replies; 14+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2021-06-11 0:04 UTC (permalink / raw)
To: help-gnu-emacs
>>> How can I accede to the initarg of an eieio class?
>> What do you mean exactly by that and why do you need that?
> [description of your code]
> This need adjustments but I hope you get the idea.
Sadly, this describes your code (with reference to unexplained things
like "the get-or-create of the object"), so no I don't understand what
you're doing at all, and I don't know what those values are
meant to hold, so I don't know what you mean to do.
E.g. why do you do
(oref-default 'moirai-dired-side :display-alist)
instead of
(oref-default 'moirai-dired-side 'display-alist)
?
Also, why do you need to do (oref-default 'moirai-dired-side
:display-alist), what do you expect it to return?
If you expect it to return the ((moirai-dired-p) ...)
you specified in the `defclass`, then why not do:
(defvar foo-bar '((moirai-dired-p) ...))
so you can get this value without having to ask the class?
Stefan
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: EIEIO accessing class slots
2021-06-11 0:04 ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2021-06-11 0:23 ` pillule
2021-06-11 0:59 ` pillule
2021-06-11 13:45 ` Stefan Monnier
2021-06-11 2:52 ` pillule
1 sibling, 2 replies; 14+ messages in thread
From: pillule @ 2021-06-11 0:23 UTC (permalink / raw)
To: Stefan Monnier; +Cc: help-gnu-emacs
[-- Attachment #1: Type: text/plain, Size: 1444 bytes --]
Stefan Monnier via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org> writes:
>>>> How can I accede to the initarg of an eieio class?
>>> What do you mean exactly by that and why do you need that?
>> [description of your code]
>> This need adjustments but I hope you get the idea.
>
> Sadly, this describes your code (with reference to unexplained things
> like "the get-or-create of the object"), so no I don't understand what
> you're doing at all, and I don't know what those values are
> meant to hold, so I don't know what you mean to do.
>
> E.g. why do you do
>
> (oref-default 'moirai-dired-side :display-alist)
>
> instead of
>
> (oref-default 'moirai-dired-side 'display-alist)
>
> ?
because it don't work.
however
(oref-default 'moirai-dired-side display-alist)
does work. Im confused.
> Also, why do you need to do (oref-default 'moirai-dired-side
> :display-alist), what do you expect it to return?
The default value inherited by some of the objects.
> If you expect it to return the ((moirai-dired-p) ...)
> you specified in the `defclass`, then why not do:
>
> (defvar foo-bar '((moirai-dired-p) ...))
>
> so you can get this value without having to ask the class?
Is that terrible to ask to the class ?
I attached an archive of the files incriminated; it is short,
a superclass defined in moirai-core.el and a class defined in
a temp file named moirai-eieio.el if ever that can help.
[-- Attachment #2: moirai early stage --]
[-- Type: application/gzip, Size: 50893 bytes --]
[-- Attachment #3: Type: text/plain, Size: 6 bytes --]
--
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: EIEIO accessing class slots
2021-06-11 0:23 ` pillule
@ 2021-06-11 0:59 ` pillule
2021-06-11 13:50 ` Stefan Monnier
2021-06-11 13:45 ` Stefan Monnier
1 sibling, 1 reply; 14+ messages in thread
From: pillule @ 2021-06-11 0:59 UTC (permalink / raw)
To: pillule; +Cc: help-gnu-emacs, Stefan Monnier
Also while speaking of the EIEIO manual the example given in
(eieio) Accessing Slots
for with-slots does not seems to work neither.
--
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: EIEIO accessing class slots
2021-06-11 0:04 ` Stefan Monnier via Users list for the GNU Emacs text editor
2021-06-11 0:23 ` pillule
@ 2021-06-11 2:52 ` pillule
1 sibling, 0 replies; 14+ messages in thread
From: pillule @ 2021-06-11 2:52 UTC (permalink / raw)
To: Stefan Monnier; +Cc: help-gnu-emacs
Stefan Monnier via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org> writes:
>>>> How can I accede to the initarg of an eieio class?
>>> What do you mean exactly by that and why do you need that?
>> [description of your code]
>> This need adjustments but I hope you get the idea.
>
> Sadly, this describes your code (with reference to unexplained things
> like "the get-or-create of the object"), so no I don't understand what
> you're doing at all, and I don't know what those values are
> meant to hold, so I don't know what you mean to do.
>
> E.g. why do you do
>
> (oref-default 'moirai-dired-side :display-alist)
>
> instead of
>
> (oref-default 'moirai-dired-side 'display-alist)
>
> ?
part of the confusion comes that oref already quote the slot, quoting it again results ''display-alist after macro-expansion. Not very much my taste to put confusion between values and symbols.
The second part of the confusion is the EIEIO manual which show as exemple in "(eieio) Quick Start"
(defclass person () ; No superclasses
((name :initarg :name ;; <<< a :keyword as initarg !
: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 class for tracking people I know.")
It uses keywords as :initarg so I followed the template to create my own methods and ended up with keywords as :initarg (also why showcasing :initarg if it automatically set by defclass ?)
It is empathized again in "(eieio) Slot Options"
‘:initarg’
A symbol that can be used in the argument list of the constructor
to specify a value for this slot of the new instance being created.
A good symbol to use for initarg is one that starts with a colon
‘:’.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The slot specified like this:
(myslot :initarg :myslot)
could then be initialized to the number 1 like this:
(myobject :myslot 1)
--
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: EIEIO accessing class slots
2021-06-11 0:23 ` pillule
2021-06-11 0:59 ` pillule
@ 2021-06-11 13:45 ` Stefan Monnier
2021-06-11 14:23 ` Stefan Monnier via Users list for the GNU Emacs text editor
2021-06-12 10:35 ` pillule
1 sibling, 2 replies; 14+ messages in thread
From: Stefan Monnier @ 2021-06-11 13:45 UTC (permalink / raw)
To: pillule; +Cc: help-gnu-emacs
>> (oref-default 'moirai-dired-side :display-alist)
>> instead of
>> (oref-default 'moirai-dired-side 'display-alist)
>> ?
> because it don't work.
Oh, that's right `oref` and `oref-default` are macros, which quote their
second arg, contrary to `slot-value` which is a function and is the
more "normal" accessor.
But there's no `slot-value-default`, indeed.
[ Side note: Using the slot name, as in (oref-default 'moirai-dired-side
display-alist), is more efficient than using the initarg name, as in
(oref-default 'moirai-dired-side :display-alist), which is deprecated. ]
>> Also, why do you need to do (oref-default 'moirai-dired-side
>> :display-alist), what do you expect it to return?
> The default value inherited by some of the objects.
But this is a lie :-(
The real semantics of the "default value" is a lot more murky.
It works like you expect only when the default value is defined as
a constant.
>> If you expect it to return the ((moirai-dired-p) ...)
>> you specified in the `defclass`, then why not do:
>> (defvar foo-bar '((moirai-dired-p) ...))
>> so you can get this value without having to ask the class?
> Is that terrible to ask to the class ?
To the extent that it doesn't exist in CLOS, then I think it's better to
avoid using this functionality ;-)
Stefan
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: EIEIO accessing class slots
2021-06-11 0:59 ` pillule
@ 2021-06-11 13:50 ` Stefan Monnier
0 siblings, 0 replies; 14+ messages in thread
From: Stefan Monnier @ 2021-06-11 13:50 UTC (permalink / raw)
To: pillule; +Cc: help-gnu-emacs
pillule [2021-06-11 02:59:33] wrote:
> Also while speaking of the EIEIO manual the example given in
> (eieio) Accessing Slots
> for with-slots does not seems to work neither.
Indeed, there's a typo:
(defclass myclass () (x :initform 1))
should be
(defclass myclass () ((x :initform 1)))
This typo has been with us for more than 10 years ;-)
Thanks,
Stefan
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: EIEIO accessing class slots
2021-06-11 13:45 ` Stefan Monnier
@ 2021-06-11 14:23 ` Stefan Monnier via Users list for the GNU Emacs text editor
2021-06-11 14:29 ` Stefan Monnier via Users list for the GNU Emacs text editor
2021-06-12 10:35 ` pillule
1 sibling, 1 reply; 14+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2021-06-11 14:23 UTC (permalink / raw)
To: help-gnu-emacs
>>> If you expect it to return the ((moirai-dired-p) ...)
>>> you specified in the `defclass`, then why not do:
>>> (defvar foo-bar '((moirai-dired-p) ...))
>>> so you can get this value without having to ask the class?
>> Is that terrible to ask to the class ?
> To the extent that it doesn't exist in CLOS, then I think it's better to
> avoid using this functionality ;-)
Another reason is that this is an undocumented behavior: `oref-default`
is defined to return the value of class-allocated slots, not the "default
value" of normal slots.
Stefan
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: EIEIO accessing class slots
2021-06-11 14:23 ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2021-06-11 14:29 ` Stefan Monnier via Users list for the GNU Emacs text editor
0 siblings, 0 replies; 14+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2021-06-11 14:29 UTC (permalink / raw)
To: help-gnu-emacs
> Another reason is that this is an undocumented behavior: `oref-default`
> is defined to return the value of class-allocated slots, not the "default
> value" of normal slots.
Hmm... I see it's not really undocumented; instead different docs say
different things about what it does :-(
Stefan
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: EIEIO accessing class slots
2021-06-11 13:45 ` Stefan Monnier
2021-06-11 14:23 ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2021-06-12 10:35 ` pillule
1 sibling, 0 replies; 14+ messages in thread
From: pillule @ 2021-06-12 10:35 UTC (permalink / raw)
To: Stefan Monnier; +Cc: pillule, help-gnu-emacs
Stefan Monnier <monnier@iro.umontreal.ca> writes:
> This typo has been with us for more than 10 years ;-)
> Thanks,
Thanks you you !
>>> If you expect it to return the ((moirai-dired-p) ...)
>>> you specified in the `defclass`, then why not do:
>>> (defvar foo-bar '((moirai-dired-p) ...))
>>> so you can get this value without having to ask the class?
>> Is that terrible to ask to the class ?
>
> To the extent that it doesn't exist in CLOS, then I think it's better to
> avoid using this functionality ;-)
>
>
> Stefan
Ok I followed your advice and got it with a defcustom...
--
^ permalink raw reply [flat|nested] 14+ messages in thread
end of thread, other threads:[~2021-06-12 10:35 UTC | newest]
Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-06-10 15:20 EIEIO accessing class slots pillule
2021-06-10 19:38 ` pillule
2021-06-10 22:03 ` Stefan Monnier via Users list for the GNU Emacs text editor
2021-06-10 22:02 ` Stefan Monnier via Users list for the GNU Emacs text editor
2021-06-10 23:04 ` pillule
2021-06-11 0:04 ` Stefan Monnier via Users list for the GNU Emacs text editor
2021-06-11 0:23 ` pillule
2021-06-11 0:59 ` pillule
2021-06-11 13:50 ` Stefan Monnier
2021-06-11 13:45 ` Stefan Monnier
2021-06-11 14:23 ` Stefan Monnier via Users list for the GNU Emacs text editor
2021-06-11 14:29 ` Stefan Monnier via Users list for the GNU Emacs text editor
2021-06-12 10:35 ` pillule
2021-06-11 2:52 ` pillule
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).