Welcome! Please see the About page for a little more info on how this works.

+1 vote
in Printing by

I am using the cl-format function from the clojure.pprint library. I am using the ~:[ directive as follows:

(cl-format nil "~:[~;~a~]" arg)

When arg is nil, I get the expected empty string as a return value. However, when arg is non-nil, liike 1, I get an error: "Not enough arguments for format definition." When I pass a second arg to the cl-format function:

(cl-format nil "~:[~;~a~]" 1 2)

it prints 2, so it's acting as if it is absorbing the first argument to check for its non-nil character and then passing the second to the internal format directive.

From my reading of both the CL Hyperspec, and the gigamonkies format reference, the ~:[ should not absorb the argument but pass it on to the internal spec for printing. Is this an error in the implementation of cl-format or am I misinterpreting the spec?

I've looked at the source and it sure looks like the navigator argument to execute-sub-format on line 862 of https://github.com/clojure/clojure/blob/master/src/clj/clojure/pprint/cl_format.clj should be arg-navigator instead.

1 Answer

0 votes
by
selected by
 
Best answer

Looking at this more closely, the conditional expression consumes an argument for the condition so this is the expected behavior.

If you want to skip back and use the consumed arg, ~:* can do that:

user=> (clojure.pprint/cl-format nil "~:[~;~:*~a~]" 100)
"100"
user=> (clojure.pprint/cl-format nil "~:[~;~:*~a~]" nil)
""

Welcome to Clojure Q&A, where you can ask questions and receive answers from members of the Clojure community.
...