[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Generic functions: Specializers like `or' or `memq'?

From: Stefan Monnier
Subject: Re: Generic functions: Specializers like `or' or `memq'?
Date: Wed, 03 Nov 2021 21:31:50 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

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

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

This is not standard CLOS parlance, because CLOS does not include
a mechanism to define new kinds of specializers: you can dispatch on
(eql <VAL>) and you can dispatch on types and that's that (tho IIRC, you
can actually extend it via the MOP, potentially with the help of some
details of your Common-Lisp implementation).


reply via email to

[Prev in Thread] Current Thread [Next in Thread]