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.

Syntax

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.

Example

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

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

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

Output

The above program produces the following output.

1
clojure_reference_values.htm
Advertisements