I have a file which I always have loaded, and in it there's a
pretty-printing method. The header looks like this:
(defun pretty-print (form &optional (pprint #'pprint))
In CLOCC, there is a pretty printing method called pp:pp which I want my
pretty print method to use, but only if the package pp is loaded. So,
what I want is something like this:
... &optional (pprint (if (find-package 'pp) #'pp:pp #'pprint)))
But if I don't have package pp loaded already, then this causes a read
error. How can I get around this?
Lowell
Lowell <······@cs.ubc.ca> writes:
> ... &optional (pprint (if (find-package 'pp) #'pp:pp #'pprint)))
>
> But if I don't have package pp loaded already, then this causes a read
> error. How can I get around this?
(if (find-package 'pp)
(symbol-function (intern "PP" "PP"))
#'pprint)
Henrik Motakef <············@henrik-motakef.de> writes:
> Lowell <······@cs.ubc.ca> writes:
>
> > ... &optional (pprint (if (find-package 'pp) #'pp:pp #'pprint)))
> >
> > But if I don't have package pp loaded already, then this causes a read
> > error. How can I get around this?
>
> (if (find-package 'pp)
> (symbol-function (intern "PP" "PP"))
> #'pprint)
This is true, but the question is why you wouldn't have the PP
package loaded. In general, I think provisionally loadable stuff
like this is awful. We used to do lots more of this back when address
spaces are small, but increasingly I just don't see the point.
Note that another way to do it is to require not PP but MY-PP where
MY-PP has (a) a variable *MY-PPRINT-DISPATCH* that is initially #'pprint
and (b) a LOAD-PP function that both loads the PP package and also
sets *MY-PPRINT-DISPATCH* to PP:PP. In this way, the other package
can just (funcall my-pp:*my-pprint-dispatch* ...) and optionally use
(my-pp:load-pp) if needed.
If the reason you're worried about PP is that it's not available on
all systems (one of the few cases where conditional loading seems to be
something that's needed), making a stub version that just defines
PP:PP as a synonym for PPRINT is yet a third way.