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

0 votes
in Metadata by

what does ^:once meta applied for anon function mean?

(db-query-with-resultset* db sql params
                           (^:once fn* [rset]
                             (process-result-set rset opts))


1 Answer

+4 votes
selected by
Best answer

it promises the compielr that the lambda will only be invoked once, and thus any variables that are captured in the lexical context can be garbage collected.

This is potentially unsafe if used incorrectly, but in some contexts it's necessary to avoid unnecessarily holding onto unused memory

This example shows how it works:

user=> (let [x (Object.)] (def x (^:once fn* [] x)))
user=> (x)
#object[java.lang.Object 0x2189e7a7 "java.lang.Object@2189e7a7"]
user=> (x)