Clojure – ref-set

Clojure ref-set

In this guide, we will discuss Clojure ref-set. This function is used to set the value of a reference to a new value irrespective of whatever is the older value.

Syntax

Following is the syntax.

(ref-set refname newvalue)

Parameters − ‘refname’ is the name of the variable holding the reference value. ‘newvalue’ is the new value that needs to be associated with the reference type.

Return Value − The reference and its corresponding new 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?))
   (dosync
      (ref-set my-ref 2))
   (println @my-ref))
(Example)

Output

The above program produces the following output.

2

Next Topic : Click Here

This Post Has 2 Comments

Leave a Reply