unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#69739: 30.0.50; `type-of` is not precise enough
@ 2024-03-11 23:19 Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2024-03-12 13:37 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2024-03-12 13:58 ` Eli Zaretskii
  0 siblings, 2 replies; 20+ messages in thread
From: Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2024-03-11 23:19 UTC (permalink / raw)
  To: 69739; +Cc: monnier

Package: Emacs
Version: 30.0.50


`type-of` is supposed to return "the" type of its argument.  Given that
ELisp has a notion of subtyping, "the" type is expected to mean "the
most precise type".  This is used in `cl-generic` to decide which method
to apply, so it's important that it returns precise information.

Currently, there `type-of` fails to return precise enough information in
a few cases:

- When the argument is nil, it returns `symbol`.  The problem here is
  that `symbol` is not a subtype of `list`, whereas nil is a list.

- When the argument is a special form, a C primitive, or
  a native-compiled function it returns `subr`.  Currently our
  type hierarchy says that `subr` is a subtype of `compiled-function`
  (and hence of `function`), but a special form is *not* a function
  (it fails the `functionp` test and can't be `funcall`ed).

Currently `cl-generic` works around the first point above by using
(if FOO (type-of FOO) 'null) instead of calling `type-of` directly.

Suggestion:

    I suggest we change `type-of` to return `null` for `nil`,
    `special-form` for subrs that are special forms, `subr-primitive`
    for C primitives, and `subr-native-elisp` for native-compiled subrs.

There are a few other cases where we could improve the precision, tho
they are less important because they don't cause problems w.r.t
subtyping like the above does.

Further improvements could include:

- Return `boolean` for `t`.  This would be nice otherwise (with the
  above suggestion) `cl-generic` can dispatch on "nil is a boolean"
  but not on "t is a boolean".
- Return `keyword` for symbols that are keywords.
- Return `fixnum` or `bignum` rather than just `integer`.
  Probably not worth the trouble.
- We could go crazy and return `keyword-with-pos` for `symbols-with-pos`
  that are keywords.

Of these further improvements, only the first (return `boolean` for `t`)
seems worth the trouble.

Still, any change as suggested here would be an incompatible change, so
there's risk it'll break some code out there (`type-of` is not used very
often, but it *is* used).  Another option is to introduce a new function
which does the same as `type-of` but with changes like the ones above.
(we could even decide to give it a `cl-generic-` prefix to discourage
its use elsewhere so we can be more free to change its return value in
the future).


        Stefan






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

end of thread, other threads:[~2024-03-18 13:33 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-11 23:19 bug#69739: 30.0.50; `type-of` is not precise enough Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-03-12 13:37 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-03-12 14:40   ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-03-12 16:07     ` Rudolf Schlatte
2024-03-13 22:10       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-03-12 13:58 ` Eli Zaretskii
2024-03-12 14:43   ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-03-12 15:02     ` Eli Zaretskii
2024-03-12 15:39       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-03-13 11:55         ` Eli Zaretskii
2024-03-14 16:56           ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-03-15  9:42             ` Andrea Corallo
2024-03-15 10:54               ` Andrea Corallo
2024-03-15  9:51             ` Basil L. Contovounesios
2024-03-15 14:09               ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-03-15 15:18                 ` Eli Zaretskii
2024-03-17 22:29                   ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-03-18 12:56                     ` Eli Zaretskii
2024-03-18 13:33                       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-03-15 11:50             ` Eli Zaretskii

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