Printing a custom representation of hash table at the repl
Pascal J. Bourguignon
pjb at informatimago.com
Fri Jul 8 19:32:56 UTC 2016
Diogo Franco <diogoalexfranco at gmail.com>
writes:
> Hello,
>
> ecl does not specialize the print-object method on hash-tables:
>
> > (find-method #'print-object '() '(hash-table t))
> Debugger received error of type: SIMPLE-ERROR
> There is no method on the generic function PRINT-OBJECT that
> agrees on qualifiers NIL and specializers (HASH-TABLE T)
>
> After reading the hyperspec carefully, I think this is ok, it only
> requires it on standard-object and structure-object. However, i still
> thought that specializing on hash-table would work, but it doesn't:
>
> >> (defmethod print-object ((obj hash-table) stream) (princ "its
> working" stream))
> #<standard-method PRINT-OBJECT (#<The BUILT-IN-CLASS HASH-TABLE>
> #<The BUILT-IN-CLASS T>)>
> >> (make-hash-table)
> #<hash-table 000000000493ee40>
>
> Shouldn't we still dispatch the call to this method? And if not,
> what's the best way in ecl to customize the printable representation
> of a hash-table?
You should not.
The implementation is free to define methods for print-object on
standard classes, but you must not do so.
This is because the methods the implementation uses to print standard
classes could be broken by your method!
You can define methods for print-object, only on the classes you define
yourself.
Instead, you can define another printing function:
(defgeneric print-lisp-object (object stream)
(:method ((object t) stream)
(print-object object stream)))
define methods on print-lisp-object,
and then use PRINT-LISP-OBJECT instead of PRINT.
--
__Pascal Bourguignon__ http://www.informatimago.com/
“The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.” -- Carl Bass CEO Autodesk
More information about the ecl-devel
mailing list