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

谁说胖子不能爱 提交于 2019-12-04 15:45:39

问题


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.



来源:https://stackoverflow.com/questions/20606249/when-to-use-exclamation-mark-in-clojure-or-lisp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!