Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a question mark at the beginning of a symbol name have any special meaning in Clojure?

In this example (from here):

(defmethod event-msg-handler :chsk/recv
  [{:as ev-msg :keys [?data]}]
  (logf "Push event from server: %s" ?data)))

where ?data is vector, does the ? have any purpose or signify something?

like image 655
Reed G. Law Avatar asked Feb 11 '23 23:02

Reed G. Law


2 Answers

The question mark does not change the way that Clojure reads or evaluates the symbol. A symbol that contains a question mark is treated in exactly the same way by Clojure as a symbol that does not.

The use of the ? in the ?data symbol is therefore just a naming convention used by the author of the Sente library.

like image 135
Symfrog Avatar answered Feb 22 '23 09:02

Symfrog


Generally punctuation in symbols is a naming convention. a trailing ? usually indicates a predicate function or some boolean flag.

Examples in the core api are things like map? and number?. An example might look like this:

=> (filter number? [1 "foo" 2 :bar]) 
(1 2)
=> (remove number? [1 "foo" 2 :bar])
("foo" :bar)
=> (def debug? true)
(when debug? (println "Debugging")

a trailing ! generally indicates that some in place mutation is occuring, e.g

=>(def an-atom (atom 0))
=> @an-atom 
0
=> (reset! an-atom  10)
=> @an-atom
10

However, some libraries do attach further meaning. For example cascalog uses leading ? !! and ! to indicate some differing properties of query output vars. See The Cascalog Docs for more information

like image 22
sw1nn Avatar answered Feb 22 '23 10:02

sw1nn