Subject: bug#77725: 31.0.50; Add support for types accepted by `cl-typep' to cl-generic?

From: David Ponce <[email-masked]>
Date: Wed, 16 Apr 2025 17:42:15 +0200

[...]
> Please find attached a first attempt at a cl-types.el to be merged in
> cl-lib.  Please feel free to propose new names if mine are not good.
> 
> For now, the new `cl-deftype' is named `cl-deftype2' for testing
> without impacting the existing environment.
> 
> I tried to consistent at using the `cl-type-' prefix (or `cl--type-'
> for internals).  But some names are very close to already existing
> ones, like 'cl-type-p' vs. `cl-typep'.  There is also a collision with
> the name `cl-type-of', already used for a builtin function, so I used
> the name `cl-types-of' which also better represent that this function
> returns a list of types.
> 
> The new code is also better at handling errors.  It should detect most
> of the possible errors, and will restore the current environnement on
> error.  I also tried to improve cl-types-of.  Please read my comments
> in the code.
> 
> WDYT?

I also have a question regarding the "typeof" generalizer which is defined
like this in cl-generic:

(cl-generic-define-generalizer cl--generic-typeof-generalizer
   10 (lambda (name &rest _) `(cl-type-of ,name))
   #'cl--generic-type-specializers)

(cl-defmethod cl-generic-generalizers :extra "typeof" (type)
   "Support for dispatch on types.
This currently works for built-in types and types built on top of records."
   ;; FIXME: Add support for other "types" accepted by `cl-typep' such
   ;; as `character', `face', `keyword', ...?
   (or
    (and (symbolp type)
         (not (eq type t)) ;; Handled by the `t-generalizer'.
         (let ((class (cl--find-class type)))
           (memq (type-of class)
                 '(built-in-class cl-structure-class eieio--class)))
         (list cl--generic-typeof-generalizer))
    (cl-call-next-method)))

The proposed "cl-types-of" generalizer must have a higher priority than
"typof" to correctly takes precedence over built-in types.

But, what about the defstruct and defclass types? Shouldn't such types
have higher priority than cl-deftype and builtin types?

I wouldn't want "cl-types-of" to have too much of a performance impact on
method dispatch for defstruct and defclass types, unless it's really
necessary.