unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Generic functions: Specializers like `or' or `memq'?
@ 2021-11-03 17:17 Michael Heerdegen
  2021-11-03 18:21 ` Philip Kaludercic
  0 siblings, 1 reply; 20+ messages in thread
From: Michael Heerdegen @ 2021-11-03 17:17 UTC (permalink / raw)
  To: Emacs mailing list

Hello,

I come back to this question again and again, so it's time to just ask:

Often I want to use a certain method implementation for several very
similar cases and want to avoid code duplication, but I don't know how
to express this using the existing method specializers.

For example, I want the `head' spec to match one of several symbols who
are synonymous.  Or instead of (eql SYM) I would rather want
(memq (SYMS...)).  Or I want to combine several SPECS using `or' because
the implementation is the same.

Am I'm missing something or - this is not possible currently, right?
Would it be possible to implement such stuff then?

TIA,
Michael.



^ permalink raw reply	[flat|nested] 20+ messages in thread
* Re: Generic functions: Specializers like `or' or `memq'?
@ 2021-11-04 17:31 Eric Abrahamsen
  2021-11-05 21:17 ` Stefan Monnier via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 20+ messages in thread
From: Eric Abrahamsen @ 2021-11-04 17:31 UTC (permalink / raw)
  To: help-gnu-emacs

Stefan Monnier via Users list for the GNU Emacs text editor
<help-gnu-emacs@gnu.org> writes:

>>>> I haven't tried it myself, but it seems like you would have to use
>>>> cl-generic-generalizers (see cl-generic.el) to do so.
>>> Yes, it should be doable.  I find it a bit suspicious that this is not
>>> already available.
>> My understanding is that these more computationally-intensive
>> generalizers have the potential to seriously slow down method funcalls,
>> and so we're sort of gently discouraged from doing that.
>
> Not really, actually, no.
>
> The main problem is one of ordering between different methods when
> several methods are applicable.
> [ `cl-generic.el` solves this problem by forcing the generalizer to
>   come with a priority (a number), which makes the ordering "trivial"
>   for `cl-generic.el` but it can be hard to choose the right priority
>   to use when defining a new specializer/generalizer.  ]

Thank you for correcting my FUD!

>> Is anyone else's brain incapable of maintaining the distinction between
>> "specializer" and "generalizer"?
>
> A specializer is what you write in the `cl-defmethod`, it says this
> method is specialized for arguments of a particular type.
> It can be thought of as a type or a set of values.
>
> A generalizer is a sort of function that takes a value and returns the
> specializer to which it belongs (it "generalizes" from a value to a set
> that includes this value).  Every kind of specializer comes with
> a matching generalizer as part of its implementation.

Okay, this is helpful, but my brain is still resisting. I also read the
docstring for `cl-generic-generalizers' from time to time, and I think
if I can really understand what it's saying, I'll have a handle on it.

My earliest confusion began when I decided that if I could find the part
of the code that runs the `eql' specializer (or do we call it the `eql'
generalizer?) and then worked back from there, I would be able to make
sense of everything. But I couldn't even find the bit of code that looks
like (eql <val> <tag>)! And still can't.

[...]

> BTW, I took the term "generalizer" from the following article (from
> which I drew a fair bit of inspiration when designing `cl-generic.el`):
>
>     Generalizers: New metaobjects for generalized dispatch
>     http://research.gold.ac.uk/9924/1/els-specializers.pdf

Thanks! I'm reading this now.

Eric




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

end of thread, other threads:[~2021-11-23  8:16 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-11-03 17:17 Generic functions: Specializers like `or' or `memq'? Michael Heerdegen
2021-11-03 18:21 ` Philip Kaludercic
2021-11-03 21:10   ` Michael Heerdegen
2021-11-03 21:32     ` Eric Abrahamsen
2021-11-03 23:19       ` Michael Heerdegen
2021-11-03 23:38         ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-11-04  0:26           ` Michael Heerdegen
2021-11-04  1:31       ` Stefan Monnier via Users list for the GNU Emacs text editor
2021-11-04  2:34         ` Stefan Monnier via Users list for the GNU Emacs text editor
2021-11-04  4:01           ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-11-06 22:32         ` Michael Heerdegen
2021-11-06 23:36           ` Stefan Monnier via Users list for the GNU Emacs text editor
2021-11-07  1:48             ` Michael Heerdegen
2021-11-21 19:43               ` Stefan Monnier via Users list for the GNU Emacs text editor
2021-11-23  8:16                 ` Michael Heerdegen
  -- strict thread matches above, loose matches on Subject: below --
2021-11-04 17:31 Eric Abrahamsen
2021-11-05 21:17 ` Stefan Monnier via Users list for the GNU Emacs text editor
2021-11-05 21:36   ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-11-06 23:29     ` Stefan Monnier via Users list for the GNU Emacs text editor
2021-11-06 23:52       ` Emanuel Berg 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).