unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* EIEIO default constructor function
@ 2021-04-03  3:50 Mario Lang
  2021-04-03 13:48 ` Stefan Monnier
  0 siblings, 1 reply; 8+ messages in thread
From: Mario Lang @ 2021-04-03  3:50 UTC (permalink / raw)
  To: emacs-devel

Hi.

While porting a CLOS based project over to Elisp, I notice that there
seems to be no way to disable the creation of a default constructor
function with `defclass'.  I am getting a redefinition warning because I
am defining my own "constructor" with the same name.  This is all just
naming-fu, i.e., I know I could just rename the class to avoid the
conflict.  But I am wondering: `make-instance' is a nice way of avoiding
namespace pollution if several classes are involved.  In other words,
avoiding the creation of yet another function might be desireable in
some situations.

So my question is, what is the rationale behind *always* creating a
wrapper for `make-instance'?  Am I missing something important?
Could we perhaps add some sort of class-option to disable the wrapper
creation?

-- 
CYa,
  ⡍⠁⠗⠊⠕



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

* Re: EIEIO default constructor function
  2021-04-03  3:50 EIEIO default constructor function Mario Lang
@ 2021-04-03 13:48 ` Stefan Monnier
  2021-04-04  2:42   ` Mario Lang
  0 siblings, 1 reply; 8+ messages in thread
From: Stefan Monnier @ 2021-04-03 13:48 UTC (permalink / raw)
  To: Mario Lang; +Cc: emacs-devel

> So my question is, what is the rationale behind *always* creating a
> wrapper for `make-instance'?

You'll have to ask Eric Ludlam ;-)

> Could we perhaps add some sort of
> class-option to disable the wrapper creation?

We could, yes.  I suggest you `M-x report-emacs-bug` so we don't forget.
Ideally attach a patch ;-)
We may also want to make those constructor functions conditional on
`eieio-backward-compatibility`.

You might also consider using `cl-defstruct`, which
doesn't suffer from such problems and is lighter weight.


        Stefan




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

* Re: EIEIO default constructor function
  2021-04-03 13:48 ` Stefan Monnier
@ 2021-04-04  2:42   ` Mario Lang
  2021-04-04  3:10     ` Stefan Monnier
  0 siblings, 1 reply; 8+ messages in thread
From: Mario Lang @ 2021-04-04  2:42 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: ericludlam, emacs-devel

Stefan Monnier <monnier@iro.umontreal.ca> writes:

>> So my question is, what is the rationale behind *always* creating a
>> wrapper for `make-instance'?
>
> You'll have to ask Eric Ludlam ;-)

I guessed as much :-).  I should probably have put him on CC.

>> Could we perhaps add some sort of
>> class-option to disable the wrapper creation?
>
> We could, yes.  I suggest you `M-x report-emacs-bug` so we don't forget.
> Ideally attach a patch ;-)

If a patch is welcome, I might look into that.

> We may also want to make those constructor functions conditional on
> `eieio-backward-compatibility`.

Oh, that is a good idea.  According to my test, CLOS does not create a wrapper by
default, so hiding that behaviour behind `eieio-backward-compatibility'
seems like what I would want to aim for.

> You might also consider using `cl-defstruct`, which
> doesn't suffer from such problems and is lighter weight.

I was actually trying that at first, but it turned out I need
inheritance based dynamic dispatch.  So I am actually very grateful for eieio
being merged into Emacs proper, `defclass' *is* handy if you need its
power.

-- 
CYa,
  ⡍⠁⠗⠊⠕



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

* Re: EIEIO default constructor function
  2021-04-04  2:42   ` Mario Lang
@ 2021-04-04  3:10     ` Stefan Monnier
  2021-04-04  8:39       ` Mario Lang
  0 siblings, 1 reply; 8+ messages in thread
From: Stefan Monnier @ 2021-04-04  3:10 UTC (permalink / raw)
  To: Mario Lang; +Cc: ericludlam, emacs-devel

>> You might also consider using `cl-defstruct`, which
>> doesn't suffer from such problems and is lighter weight.
> I was actually trying that at first, but it turned out I need
> inheritance based dynamic dispatch.

I don't understand: cl-defmethod does inheritance-based dynamic
dispatch for defstructs just as it does for defclasses.


        Stefan




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

* Re: EIEIO default constructor function
  2021-04-04  3:10     ` Stefan Monnier
@ 2021-04-04  8:39       ` Mario Lang
  2021-04-04 14:12         ` Stefan Monnier
  0 siblings, 1 reply; 8+ messages in thread
From: Mario Lang @ 2021-04-04  8:39 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: emacs-devel

Stefan Monnier <monnier@iro.umontreal.ca> writes:

>>> You might also consider using `cl-defstruct`, which
>>> doesn't suffer from such problems and is lighter weight.
>> I was actually trying that at first, but it turned out I need
>> inheritance based dynamic dispatch.
>
> I don't understand: cl-defmethod does inheritance-based dynamic
> dispatch for defstructs just as it does for defclasses.

Maybe I was using sloppy terminology, but as far as I can see, `cl-defstruct'
can not have a parent relationship, so what I call inheritance-based
dispatch isn't really possible with cl-defstruct?

(defclass foo () ())
(cl-defmethod val ((foo foo)) 0)
(defclass bar (foo) ())
(cl-defmethod val ((bar bar)) 1)

-- 
CYa,
  ⡍⠁⠗⠊⠕



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

* Re: EIEIO default constructor function
  2021-04-04  8:39       ` Mario Lang
@ 2021-04-04 14:12         ` Stefan Monnier
  2021-04-08  3:37           ` Mario Lang
  0 siblings, 1 reply; 8+ messages in thread
From: Stefan Monnier @ 2021-04-04 14:12 UTC (permalink / raw)
  To: Mario Lang; +Cc: emacs-devel

> Maybe I was using sloppy terminology, but as far as I can see, `cl-defstruct'
> can not have a parent relationship, so what I call inheritance-based
> dispatch isn't really possible with cl-defstruct?
>
> (defclass foo () ())
> (cl-defmethod val ((foo foo)) 0)
> (defclass bar (foo) ())
> (cl-defmethod val ((bar bar)) 1)

    (cl-defstruct (my-foo)
      slot1)
    (cl-defmethod my-val ((x my-foo)) (my-foo-slot1 x))
    (cl-defstruct (my-bar
                   (:include my-foo))
      slot2)
    (cl-defmethod my-val ((x my-bar))
      (+ (my-bar-slot2 x) (cl-call-next-method)))

     (my-val (make-my-bar :slot1 4 :slot2 5))
     ==> 9


-- Stefan
      




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

* Re: EIEIO default constructor function
  2021-04-04 14:12         ` Stefan Monnier
@ 2021-04-08  3:37           ` Mario Lang
  2021-04-08  4:44             ` Stefan Monnier
  0 siblings, 1 reply; 8+ messages in thread
From: Mario Lang @ 2021-04-08  3:37 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: emacs-devel

Hi Stefan.

Thanks a lot.  :include is just what I was looking for.
And while `make-instance' and `slot-value' are convenient, I guess I can
work around not having them.  Having `initialize-instance' would
also have been convenient, but since I apparently only needed an :after
method I ended up using the :constructor option to rename the generated
constructor to indicate it is internal.
So it appears I can actually do without `defclass' and friends.

Stefan Monnier <monnier@iro.umontreal.ca> writes:

>> Maybe I was using sloppy terminology, but as far as I can see, `cl-defstruct'
>> can not have a parent relationship, so what I call inheritance-based
>> dispatch isn't really possible with cl-defstruct?
>>
>> (defclass foo () ())
>> (cl-defmethod val ((foo foo)) 0)
>> (defclass bar (foo) ())
>> (cl-defmethod val ((bar bar)) 1)
>
>     (cl-defstruct (my-foo)
>       slot1)
>     (cl-defmethod my-val ((x my-foo)) (my-foo-slot1 x))
>     (cl-defstruct (my-bar
>                    (:include my-foo))
>       slot2)
>     (cl-defmethod my-val ((x my-bar))
>       (+ (my-bar-slot2 x) (cl-call-next-method)))
>
>      (my-val (make-my-bar :slot1 4 :slot2 5))
>      ==> 9
>
>
> -- Stefan

-- 
CYa,
  ⡍⠁⠗⠊⠕



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

* Re: EIEIO default constructor function
  2021-04-08  3:37           ` Mario Lang
@ 2021-04-08  4:44             ` Stefan Monnier
  0 siblings, 0 replies; 8+ messages in thread
From: Stefan Monnier @ 2021-04-08  4:44 UTC (permalink / raw)
  To: Mario Lang; +Cc: emacs-devel

> Thanks a lot.  :include is just what I was looking for.
> And while `make-instance' and `slot-value' are convenient, I guess I can
> work around not having them.

I'd welcome patches to make them work with cl-structs.
For `slot-value` this should be quite easy.  For `make-instance` there
are some choices to be made about the semantics we want to offer, so
maybe it's not harder to implement but it requires more thought.

> Having `initialize-instance' would
> also have been convenient, but since I apparently only needed an :after
> method I ended up using the :constructor option to rename the generated
> constructor to indicate it is internal.

Note you can also use things like:

    (cl-defstruct (my-foo
                   (:constructor nil)
                   (:constructor my-constructor-name
                     (slot1 slot2 &aux (_ (when (> slot1 slot2))
                                            (error "blabla")))))
      slot1 slot2)

which might cover some of that ground as well.
I find those `&aux` thingies hideous, tho, so don't take this as
a recommendation.

AFAIK the only thing really missing is the ability to call-next-method
to the parent's constructor, so the children's constructor either have
to duplicate the work of their parent, or they have to manually call
some ad-hoc function that encapsulates the work of the
parent's constructor.
I can imagine cases where it increases annoyingly the coupling between
the parent and the child types, tho in my experience, this coupling is
pretty much always tight for other reasons anyway, so it's never been
a problem for me.


        Stefan




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

end of thread, other threads:[~2021-04-08  4:44 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-03  3:50 EIEIO default constructor function Mario Lang
2021-04-03 13:48 ` Stefan Monnier
2021-04-04  2:42   ` Mario Lang
2021-04-04  3:10     ` Stefan Monnier
2021-04-04  8:39       ` Mario Lang
2021-04-04 14:12         ` Stefan Monnier
2021-04-08  3:37           ` Mario Lang
2021-04-08  4:44             ` Stefan Monnier

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

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).