-
Notifications
You must be signed in to change notification settings - Fork 0
Variables
AxelHumeau edited this page Jan 14, 2024
·
2 revisions
There is two way to define a variable:
-
=
: classic define method, take the name of the variable at the left and the value at the left -
fn
: define method use only on functions (more details on the Functions page)
When a variable doesn't exist it is created upon the use of =
or fn
, if it exist it is updated.
bar = 5 #define a variable
bar # returns 5
bar = 9 #update the variable
bar # returns 9
Variables are organised in scopes, a new scope is created when a function is called. A varible can only be accessed and modified from it's original scope. When creating a variable outside of a function, the variable is considered global can be accessed from all scopes.
bar = 5 # define a variable
bar # returns 5
fn changeGlobal(||) {|
bar = 7 # define a global variable
foo = 9
|}
changeGlobal(||)
bar # returns 7
foo # throw an error
All variables are mutable and can change type
That means a variable can go from an Int
to a String
to a Function
bar = 5 #define a variable
bar # returns 5
bar = "Hello World"
bar # return "Hello World"
bar = true
bar # return true
fn bar(||) {|
5
|}
bar # return a function