0

I want to switch some behavior in my .emacs based on whether I have the new native fast json-serialize which is implemented in C rather than Elisp.

Is there a simple way to detect whether a function is implemented in C rather than Elisp? I assume there must be because C-h f json-serialize knows it.

2

I did C-h f C-h f and got a *Help* buffer describing describe-function as a "Lisp function" defined in help-fns.el. I clicked on the file name and searched for "Lisp function" which lead me to the function help-fns-function-description-header whose code indicated that what we want is subrp.

(subrp (symbol-function 'car))
==> nil

A bit more consideration would point to help-fns--analyze-function which disentangles things like aliasing and advising.

I think, however, that you should look at the "source" rather than the result here, i.e., check that the lisp file where json-serialize could be defined has been loaded - use featurep for that.

2
(subrp (symbol-function 'json-serialize))

should do it. See Functions for details.

  • Of course, it will report the "wrong" result if json-serialize is implemented in C but is currently advised (or if you applied to it trace-function or debug-on-entry or elp-instrument-function, or ...) – Stefan Oct 11 at 15:23
  • @Stefan: Maybe put that info in another answer? Seems like it would be helpful, if elaborated a bit. – Drew Oct 11 at 16:06
  • I suppose I could just run a timing test! (1/2 kidding) – GaryO Oct 11 at 17:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.