-
Notifications
You must be signed in to change notification settings - Fork 4
Variables
Variables in MeowScript are lexical scoped, dynamic typed and mutable.
To create a new variable you use the new
command.
new num 12
new str "hello"
new lst [1,2,3]
new c
This created 4 variables
variable | type | value |
---|---|---|
num | Number | 12 |
str | String | "hello" |
lst | List | [1,2,3] |
c | Number | 0 |
set
sets a variable that already exists to a value, even if it's above the current scope.
If the variable does not exit yet, it acts just like new
.
new a 12
{
set a 22
set b 42
}
a
is now 22, and b
is out of scope already.
To create immutable variables you use const
. This variables can not be changed later on.
const PI 3.14
set PI "wha" # error!
You can get the typename as string from something using the typeof
command.
new var "cool things"
typeof 12 # "Number"
typeof var # "String"
In MeowScript are commands to cast one type to another.
These functions don't crash if they fail, but return a void
type which can be checked like this:
typeof {number("abc")} # "Void"
Converts its argument into a string.
string(12) # "12"
string([1,2,3]) # "[1,2,3]"
string("something") # "something"
Converts its argument into a number.
number("12.34") # 12.34
number("-0.13") # -0.13
Converts its argument into a string.
list(5) # [0,1,2,3,4]
list("Hi!") # ["H","i","!"]
Takes a string as input and constructs a variable using it.
str2var("[7,1,4]") # [7,1,4]
str2var("1.234") # 1.234