zcaudate
zcaudate

Reputation: 14258

How is the set! Function implemented in clojure

In examples, I see

(set! *unchecked-math* true)

and then operations are done. However, what exactly in the function set! And how come it is allowed to mutate unchecked-math which is a boolean?

Upvotes: 0

Views: 102

Answers (2)

Michał Marczyk
Michał Marczyk

Reputation: 84331

To explain why set! works on *unchecked-math*:

*unchecked-math* is a dynamic Var for which the compiler installs a thread-local binding before it actually starts compiling. It is this thread-local binding that is set to true by (set! *unchecked-math* true). *warn-on-reflection* works similarly.

The initial value of the compiler's binding is simply whatever is obtained by derefing the Var. In particular, if the compiler is called upon to compile code on a thread which already has its own bindings for the dynamic Vars relevant to the compilation process, the compiler will use the values of those bindings; that is, it will still install its own bindings, but it will use the current values.

Upvotes: 1

tom
tom

Reputation: 19153

set! is a special form (i.e. neither a function nor a macro) which sets the value of thread-a local-bound dynamic Var, or a Java instance/static field.

set! is implemented in Java as part of the core language: Var.java on GitHub.

You should read up on Var and set! on clojure.org, as Ankur points out in his comment: http://clojure.org/vars#set

Upvotes: 3

Related Questions