When to use exclamation mark in clojure (or lisp)?

2019-03-24 01:00发布

问题:

They say that use exclamation marks when naming impure functions.

But I don't exactly understand the "impure" functions. Are they

  • functions change state of their arguments (via reset!, alter, java-object-methods, ...)
  • functions occur side-effect (for example, print, spit, ...)
  • or both?

Obviously, official clojure apis don't have bang!s on every case above. I wonder when should I put them and need your help to make my code saner.

回答1:

I would say you don't need to put ! on every impure function. Community Clojure Style Guide recommends:

The names of functions/macros that are not safe in STM transactions should end with an exclamation mark.

So, basically, end with ! functions that change state for atoms, metadata, vars, transients, agents and io as well.

Thanks to @noisesmith for update.