Hello All,
I encountered the following behavior in Clojure 1.6 and wanted to check if it should be considered a bug or not. I would say yes but wanted to double check on the list first.
Here's a minimal test case that elicited the error:
(recur (next xs) (+ (first xs) acc))))
(f [1 2 3 4] 0) => 10
Now, if I want to add pre/post conditions, the following happens:
[xs acc]
{:pre [(or (nil? xs) (sequential? xs)) (number? acc)]
:post [number?]}
(if (nil? xs)
acc
(recur (next xs) (+ (first xs) acc))))
=> this fails to compile with "CompilerException java.lang.UnsupportedOperationException: Can only recur from tail position"
In fact, it is only the post-condition that triggers the issue.
My guess would be that the recur statement is being knocked out of tail position by the mechanism for handling the post-condition assertion. It can be fixed in the code by adding an explicit loop:
{:pre [(or (nil? xs) (sequential? xs)) (number? acc)]
(recur (next xs) (+ (first xs) acc)))))
Thanks,
Michael O'Keefe