I have created a function with an optional argument a with a default value corresponding to the result of a function call :
(defn mycall[]
(print "*** called")
100)
(defn hello [& {:keys [a] :or {a (mycall)}}]
(print a))
Upon running my function, I find that the function call for default is evaluated EVEN if I provide a value for the argument 'a'
(hello :a 22)
*** called
22
Why does this happen? I would have expected not printout '*** called' and is there a way to not have the default function evaluation if I supply the named parameter?
Does anyone have any suggestion to implement this differently ? Many thanks.
During destructuring,
{:keys [a] :or {a (mycall)}}becomes something likeIt uses the arity of
getthat accepts a default value. Sincegetis a plain function, its arguments always have to be evaluated before the function itself is called.Yes, albeit it's not that straightforward if you want for the default value to be evaluated only once:
If you don't care how many times the default value is evaluated or if you need to have it evaluated whenever
:ais missing, simply remove the delay altogether and put(mycall)where@default-a'is.