How do I declare a variable that cannot be changed?

Suppose I want to create a variable that cannot be changed after being initialized, for
example, here is the Java equivalent:

final int emp_id

>Solution :

You can use the def keyword to bind a value to a symbol, and that value cannot change

(def emp_id 1)
(set emp_id 2) # Output: compile error: cannot set constant

If you know your value will change later on, for example, and employee last name after marriage, then use the var keyword:

(var emp_last_name "Smith")
(set emp_last_name "Summer")

emp_last_name # Output: "Summer"

Leave a Reply