[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
bug#69739: 30.0.50; `type-of` is not precise enough
From: |
Stefan Monnier |
Subject: |
bug#69739: 30.0.50; `type-of` is not precise enough |
Date: |
Mon, 11 Mar 2024 19:19:40 -0400 |
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