unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Any convenient way for cl-generic to dispatch on a callable Lisp Object?
@ 2020-04-20  8:55 Zhu Zihao
  2020-04-20 13:44 ` Stefan Monnier
  0 siblings, 1 reply; 4+ messages in thread
From: Zhu Zihao @ 2020-04-20  8:55 UTC (permalink / raw)
  To: emacs-devel

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?




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

* Re: Any convenient way for cl-generic to dispatch on a callable Lisp Object?
  2020-04-20  8:55 Any convenient way for cl-generic to dispatch on a callable Lisp Object? Zhu Zihao
@ 2020-04-20 13:44 ` Stefan Monnier
  2020-04-20 15:32   ` Zhu Zihao
  2020-04-20 15:39   ` Zhu Zihao
  0 siblings, 2 replies; 4+ messages in thread
From: Stefan Monnier @ 2020-04-20 13:44 UTC (permalink / raw)
  To: Zhu Zihao; +Cc: emacs-devel

> 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




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

* Re: Any convenient way for cl-generic to dispatch on a callable Lisp Object?
  2020-04-20 13:44 ` Stefan Monnier
@ 2020-04-20 15:32   ` Zhu Zihao
  2020-04-20 15:39   ` Zhu Zihao
  1 sibling, 0 replies; 4+ messages in thread
From: Zhu Zihao @ 2020-04-20 15:32 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: Zhu Zihao, emacs-devel

Thanks for your reply.

I add a custom generalizer for funcall-able Lisp Object now

(cl-generic-define-generalizer rstream--callable-generalizer
  51 (lambda (name &rest _)
       `(when (functionp ,name)
          'rstream-callable))
  (lambda (tag &rest _)
    (when (eq tag 'rstream-callable)
      (list tag))))

(cl-defmethod cl-generic-generalizers ((_specializer (eql rstream-callable)))
  "Support for `stream' specializers."
  (list rstream--callable-generalizer))





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

* Re: Any convenient way for cl-generic to dispatch on a callable Lisp Object?
  2020-04-20 13:44 ` Stefan Monnier
  2020-04-20 15:32   ` Zhu Zihao
@ 2020-04-20 15:39   ` Zhu Zihao
  1 sibling, 0 replies; 4+ messages in thread
From: Zhu Zihao @ 2020-04-20 15:39 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: Zhu Zihao, emacs-devel

Maybe we can add a generalizer based on `cl-typep`. So the the complexity of
tagcode expression can be determined by user.




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

end of thread, other threads:[~2020-04-20 15:39 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-04-20  8:55 Any convenient way for cl-generic to dispatch on a callable Lisp Object? Zhu Zihao
2020-04-20 13:44 ` Stefan Monnier
2020-04-20 15:32   ` Zhu Zihao
2020-04-20 15:39   ` Zhu Zihao

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