Skip to content

Variables

LabRicecat edited this page Nov 25, 2022 · 6 revisions

Variables in MeowScript are lexical scoped, dynamic typed and mutable.

new

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

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.

const

To create immutable variables you use const. This variables can not be changed later on.

const PI 3.14

set PI "wha" # error!

typeof

You can get the typename as string from something using the typeof command.

new var "cool things"

typeof 12 # "Number"
typeof var # "String"

Type Casts

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"

string()

Converts its argument into a string.

string(12) # "12"
string([1,2,3]) # "[1,2,3]"
string("something") # "something"

number()

Converts its argument into a number.

number("12.34") # 12.34
number("-0.13") # -0.13

list()

Converts its argument into a string.

list(5) # [0,1,2,3,4]
list("Hi!") # ["H","i","!"]

str2var

Takes a string as input and constructs a variable using it.

str2var("[7,1,4]") # [7,1,4]
str2var("1.234") # 1.234

>>> Next Article

Clone this wiki locally