[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Any convenient way for cl-generic to dispatch on a callable Lisp Obj
From: |
Stefan Monnier |
Subject: |
Re: Any convenient way for cl-generic to dispatch on a callable Lisp Object? |
Date: |
Mon, 20 Apr 2020 09:44:23 -0400 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) |
> cl-generic can dispatch on builtin types, so subr and complied-function can be
> dispatched. But looks that it lacks support of dispatching on funcall-able
> Lisp
> Object(e.g. dynamic constructed lambda expression, closure, a symbol with
> function slot set). Can we add this?
The reason I haven't is that I felt that it would add too much extra
cost to the dispatch. Currently the computation of the "tag" on which
to dispatch for built-in types is (as you can see in
`cl--generic-typeof-generalizer`):
(if ,name (type-of ,name) 'null)
We'd have to change this to distinguish generic `cons` from "cons cell
with a `lambda` or `closure` in its `car`".
I did implement it in Elisp locally, but I felt that it makes the
computation of the tag a bit too expensive for my taste.
I guess if we do it in C (by implementing a new `type-of-for-cl-generic`
built-in function), it would be cheap enough.
But next thing we know someone will want to dispatch on `face` or
`keymap`, and then we'd need to distinguish
symbol, symbol-and-function, symbol-and-function-and-face-and-keymap
symbol-and-function-and-face-but-not-keymap,
symbol-and-function-and-keymap-but-not-face, ...
which will quickly become tiresome.
Another way to make it cheaper is to rework the way generalizers work
such that the code that builds the tag-computation can know which
specializers we're interested in, so that we could use just `type-of` when
there's no dispatch on `function` and use a more costly computation for
those rare places where we do dispatch on `function`.
Stefan