Skip to content

Latest commit

 

History

History

14-object-and-arrays

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Object and Array

Wes took a look at assignment v. reference in JavaScript for this lesson. I used this lesson to explore the equivalent concept in CLJ/S:

  1. Terminology
  2. Symbols
  3. Vars
  4. Assignment vs. Binding

Lesson

I work as a developer professionally and I found this lesson refreshing because it can be easy to forget how these seemingly "basic" concepts are difficult for professional and new developers alike to fully grasp. This being said, these are notes that helped me sort this out. Consider the challenge that is explaining the difference between a key, Symbol and Var. I believe a core challenge here is that someone new is looking at them they would immediatley think they are all just Strings With this said, this lesson is not done and I will likely revisit this several more times to improve and clarify where possible, but for now, I feel it is a good start.

Resources

Getting into the nitty-gritty's of all this can be very challenging. What I have provided below is a summation of my understanding of these things. For stronger technical explainations see the following resources:

Terminology

These are the terms used in Clojure. They are not interchangeable, rather they each refer to one clearly defined idea. The spelling is important. If I get the spelling wrong in this document, I likely messed up.

  • Var
  • Symbol
  • Unqualified Symbol
  • Data Type
  • resolved Symbol
  • value
  • root binding
  • dynamic thread-local binding
  • unbound
  • local scope
  • global scope
  • scope

Vars

This is how you declare a Var in Clojure:

(def my-name "Jerald")

;; #'user/my-name
  • A Var is a mutable container
  • A Var declared with def is going to always become global in your namespace
  • A Var references its binding (value). In the above, my-name (var) refernces "Jerald" (binding)
  • A Var can have two types of binding: root binding and dynamic thread-local binding
  • A Var can be of several sub-types: static, dynamic, free

The root binding

All Vars and their subtypes have a root binding. A root binding is the initial value associated with your variable. For example:

(def name "Thomas")

In the above, "Thomas" is the root binding. Having said this, you do not need to bind a Var with a root value. Consider;

(def name)

In the above example, the Var above was not bound to a value when it was created, so we consider this unbound.

Var search order

We will look in local scope first and than move to the global scope

Re-bind Vars

(def name "Thomas")

(def name "Kate")

The variable "name" is not destroyed and re-created, but rather the Var is re-bound to "Kate".

Symbols

A Symbol is Data Type. Here are some examples of Symbols:

(def my-name "Jerald")

(+ 1 2)

In the above, + and my-name are Symbols. Comparatively, 1 and 2 are Numbers which are a different kind of Data Type. This could be confusing because in above section we said that my-name is a Var. The truth is that the reaility is a little more complex and for most of your development process, does not really matter, but keep the following in mind:

  • my-name is a Symbol which references a Var
  • When we reference a Symbol the symbol knows where to find a Var and the Var is going to return it's binding.

This distinction is going to help when / if you really need to dive into CLJ/S code. For a great overview of this, please see this article

Assignment vs Binding

When we talk about variables in clojure we do not say assigning, we actually say binding. I like to think that one of the key differences is how assigning vs. binding looks. In JS, you would do this to create a variable:

var myName = "Jay"

In the above example, the = is an example of an assignment operator. Clojure does not have assignment operators as you can see in the way we declare a Var.

(def my-name "Jay")

This seems to be the only way to see the differene in syntax. When it comes to Clojure internals, there is likely other differences, but for now, the takeaway is we don't assign in CLJ/S we bind.