Reed G. Law
Reed G. Law

Reputation: 3945

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?

Upvotes: 5

Views: 2136

Answers (2)

Symfrog
Symfrog

Reputation: 3418

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.

Upvotes: 5

sw1nn
sw1nn

Reputation: 7328

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

Upvotes: 3

Related Questions