Clojure - ref

This is used to create a reference value. When creating a reference value, there is an option to provide a validator function, which will validate the value created.


Following is the syntax.

(ref x options)

Parameters − ‘x’ is the value which needs to be provided to the reference. ‘Options’ is a set of options that can be provided, such as the validate command.

Return Value − The reference and its corresponding value.


An example on how this is used is shown in the following program.

(ns clojure.examples.example
(defn Example []
   (def my-ref (ref 1 :validator pos?))
   (println @my-ref))

To access the value of the reference value, you can use the @ symbol.


The above program produces the following output.

Kickstart Your Career

Get certified by completing the course

Get Started