An Introduction to Dragon/Lessons/Variables
Variables
[edit | edit source]To create a new variable, you just need to determine the variable name & value. The value will determine the variable type and you can change the value to switch between the types using the same variable name.
Syntax:
<Variable Name> = <Value>
.. tip::
The operator '=' is used here as an assignment operator and the same operator can be used in conditions, but for testing equality of expressions.
.. note::
The variable will contain the real value (not a reference). This means that once you change the variable value, the old value will be removed from memory (even if the variable contains a list or object).
Dynamic Typing
[edit | edit source]Dragon uses dynamic typing:
x = "Hello" // x is a string
showln x // print list items
x = 5 // x is a number (int)
showln x
x = 1.2 // x is a number (double)
showln x
x = [1,2,3,4] // x is a list
showln x
Deep Copy
[edit | edit source]We can use the assignment operator '=' to copy variables. We can do that to copy values like strings & numbers, and even complete lists & objects! The assignment operator will do a complete duplication for us. This operation is called a deep copy.
list = [1,2,3,"four","five"]
list2 = list
list = []
show list // print the first list - no items to print
showln "********"
show list2 // print the second list - contains 5 items
Weakly Typed
[edit | edit source]Dragon is a weakly typed language. This means that the language can automatically convert between data types (like string & numbers) when that conversion makes sense.