Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the @ (at sign) mean in Clojure?

I found this line of Clojure code: @(d/transact conn schema-tx). It's a Datomic statement that creates a database schema. I couldn't find anything relevant on Google due to difficulties searching for characters like "@".

What does the 'at' sign mean before the first parenthesis?

like image 542
Felipe Avatar asked Jul 07 '14 21:07

Felipe


People also ask

What are symbols in Clojure?

In Common Lisp, a "symbol" is a location in memory, a place where data can be stored. The "value" of a symbol is the data stored at that location in memory. In Clojure, a "symbol" is just a name. It has no value.

What does -> mean in Clojure?

It's a way to write code left to right, instead of inside out, e.g. (reduce (map (map xs bar) foo) baz) becomes (-> xs (map bar) (map foo) (reduce baz))

What is clojure VAR?

Clojure is a practical language that recognizes the occasional need to maintain a persistent reference to a changing value and provides 4 distinct mechanisms for doing so in a controlled manner - Vars, Refs, Agents and Atoms.


3 Answers

This is the deref macro character. What you're looking for in the context of Datomic is at:

http://docs.datomic.com/transactions.html

under Processing Transactions:

In Clojure, you can also use the deref method or @ to get a transaction's result.

For more on deref in Clojure, see:

http://clojuredocs.org/clojure_core/clojure.core/deref

like image 67
Ben Kamphaus Avatar answered Oct 18 '22 00:10

Ben Kamphaus


Here is a useful overview of Clojure default syntax and "sugar" (i.e. macro definitions).

http://java.ociweb.com/mark/clojure/article.html#Overview

You'll find explained the number sign #, which indicates regex or hash map, the caret ^, which is for meta data, and among many more the "at sign" @. It is a sugar form for dereferencing, which means you get the real value the reference is pointing to.

Clojure has three reference types: Refs, Atoms and Agents.

http://clojure-doc.org/articles/language/concurrency_and_parallelism.html#clojure-reference-types

Your term @(d/transact conn schema-tx) seems to deliver a reference to an atom, and by the at sign @ you defer and thus get the value this reference points to.

BTW, you'll find results with search engines if you look e.g. for "Clojure at sign". But it needs some patience ;-)

like image 27
peter_the_oak Avatar answered Oct 18 '22 00:10

peter_the_oak


The @ is equivalent to deref in Clojure. transact returns a future which you deref to get the result. deref/@ will block until the the transaction completes/aborts/times out.

like image 37
mac Avatar answered Oct 17 '22 22:10

mac