Clojure language 6: def & VaR

Source: Internet
Author: User
Tags vars

Clojure provides an operation form other than functions and macros. Special form is a limited number and is not implemented by clojure itself.

It mainly includes the following:

Catch, def, do, dot ('.'), finally, FN, if, let, loop,

Monitor-enter, monitor-exit, new, quote, recur, set !, Throw, try, and var.

Complete documentation see: http://clojure.org/special_forms

Vars should be explained here. First, clojure is a functional programming language, so it does not support variables and only supports immutable ). The advantage is that the function state is not affected by global variables and does not need to be locked during concurrent programming. So what should we do if clojure needs to support "variables" in practice? Provides four mechanisms, one of which is vars.

VaR can point to a mutable storage location. The binding function is to point a VaR to a mutable storage location. If this var no longer points to any mutable storage location, it is called unbound.


Def special form is used to create a VaR and bind it to a root value. For example, the following code:

user=> (def x 5)#'user/x

Here, parameter 5 is provided as the initial value, so X is created and initialized to 5. You can check the value of X as follows:

user=> x5


If the initial value parameter is not provided, def Creates var but does not bind it, that is, the unbound status:

user=> (def y)#'user/yuser=> y#<Unbound Unbound: #'user/y>


VaR has a concept of root binding. That is, the binding shared by all threads. If the thread has its own binding, the root binding is no longer used.

By default, A Var is static and can be accessed by all threads. If you want to create a thread's own binding and other threads cannot access it, you should do two things:

1. Def specify the parameter ^: dynamic when creating VaR

2. Use the binding macro to create the binding of your own thread

See the official document for example: http://clojure.org/vars

By default Vars are static, but per-thread bindings for Vars defined with metadata marking them as dynamic can be established via the macro binding and within-thread they obey a stack discipline:user=> (def ^:dynamic x 1)user=> (def ^:dynamic y 1)user=> (+ x y)2 user=> (binding [x 2 y 3]         (+ x y))5 user=> (+ x y)2

Http://java.ociweb.com/mark/clojure/article.html#Vars this article is also good.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.