Skip to content

Commit

Permalink
Custom writer for function objects
Browse files Browse the repository at this point in the history
When printing function objects, display its lambda-list.

This could be done via PRINT-OBJECT in theory, but PRINT-OBJECT is not
being dispatch from WRITE function. See issue jscl-project#444

Example of how functions are printed after this commit:

```
  CL-USER> #'prin1-to-string
  #<FUNCTION PRIN1-TO-STRING (FORM)>
  CL-USER> #'prin1
   <FUNCTION PRIN1 (FORM &OPTIONAL STREAM)>
```
  • Loading branch information
mmontone committed Sep 1, 2022
1 parent e76ab86 commit cb21040
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/print.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,18 @@
" :count ~d>")
(hash-table-count form)))

;; function object printer
(defun function-object-printer (form stream)
(let ((res))
(setq res (concat "#<FUNCTION "
(or (jscl::oget form "fname")
"")
" "
(or (and (jscl::function-lambda-list form)
(princ-to-string (jscl::function-lambda-list form))) "")
">"))
(simple-format stream res)))

#+jscl
(defun write (form &key (stream *standard-output*))
(cond ((mop-object-p form)
Expand All @@ -339,7 +351,9 @@
(invoke-object-printer #'hash-table-object-printer form stream))
((structure-p form)
(invoke-object-printer #'structure-object-printer form stream))
(t (let ((stream (output-stream-designator stream)))
((functionp form)
(invoke-object-printer #'function-object-printer form stream))
(t (let ((stream (output-stream-designator stream)))
(multiple-value-bind (objs ids)
(scan-multiple-referenced-objects form)
(write-aux form stream objs ids)
Expand Down

0 comments on commit cb21040

Please sign in to comment.