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

0 votes
in Clojure by

Volatiles and vswap! were added in 1.7 as performant mechanisms to achieve uncoordinated mutation to the language.
given the fact that their addition was a performance-centric one, vswap! was implemented as a macro rather than a normal function to avoid runtime dereference of the function var and the optional apply overhead in case of multiple args.

However this:
-is not necessary
-breaks the api parallelism between volatile/atom swap!/vswap! reset!/vreset!
-makes impossible certain use cases (vswaps! in update-in forms)
-is potentially confusing given that swap! is a function

Infact the macro can be replaced with a function with :inline metadata.
This is a strictly additive change that will make so that for all the current valid usages of vswap! nothing will change, it will still be macroexpanded by the inliner and additionally since it is now a function it can be used in HOF contexts where it's not unusual to see swap! used.

7 Answers

0 votes
by

Comment made by: alexmiller

Nicola, please don't set the fix version on tickets.

0 votes
by

Comment made by: bronsa

Sorry, I mixed the fix version with the affected field

0 votes
by

Comment made by: glchapman

An additional drawback to the current macro is that it will result in double evaluation of the vol expression:

user=> (macroexpand '(vswap! (return-a-vol) inc)) (. (return-a-vol) reset (inc (.deref (return-a-vol))))

0 votes
by

Comment made by: alexmiller

I don't think Rich is interested in expanding the use of inline.

Re Greg's comment, that would be considered if you want to make a second ticket.

0 votes
by

Comment made by: bronsa

Not really sure I understand the adversion to using inline -- it works perfectly fine and this is the exact use-case for it: a function that we want to inline for performance reason.

I really fail to see the reasoning behind making what should be a function, a macro, just for the sake of not using inline.

0 votes
by

Comment made by: pbwolf

Adding {{:inline}} to existing functions may be deplored as an unprincpled, sloppy, desperate, tragic compromise, and a slippery slope. But vswap! is the opposite case: The macro is what already exists, somewhat idiosyncratically. Pairing it with a function would make it more wholesome, not less.

0 votes
by
Reference: https://clojure.atlassian.net/browse/CLJ-1777 (reported by bronsa)
...