-
Notifications
You must be signed in to change notification settings - Fork 8
Types
There are five supported data types and null
. Each mapping directly to a C# data type.
Integers are represented with a whole number. For example 42
.
int foo = 42
Doubles contain a decimal place. For example 4.2
.
double foo = 4.2
Strings are represented with single or double quotes.
string foo = "Hello World!"
string bar = 'That\'s escaped'
Booleans are represented as a true
or false
.
bool foo = true
Objects represent a simple key value pair where the value can be any data type.
object foo = {hello: 'world'}
object foo = {hello: ['world', '!']}
Lists are represented as type[]
and start with an index of 0. All values contained must be of the same data type.
int[] foo = [1, 2, 3]
double[] foo = [1.0, 2.0, 3.0]
string[] foo = ['foo', 'bar']
bool[] foo = [true, false]
var foo = [] # exception: ambiguous data type
var foo = int([]) # correct casting
int[] foo = [] # also correct
bar([]) # exception: ambiguous data type
bar(int([])) # always cast empty arrays
Without extending the list types with methods there are no methods for interacting with a list outside of casting between data types.
Examples of commen methods are shown in the examples below.
var list = int[1, 2] # [1, 2]
list.add(3) # [1, 2, 3]
list.remove(3) # [1, 2]
list.contains(3) # false
list.length # 2
if (foo == null):
log('foo is undefined')
Regex is used when matching parts of strings or parsing values out of strings.
regex r = /world/;
'hello world!'.match(r) # true
Flag | Description |
---|---|
i | case insensitive |
m | multiline |
s | singleline |
The var keyword allows you define variables without typing out the full data type. This syntax is considered to be more readable.
var foo = 1 # Integer
var foo = 1.0 # Double
var foo = 'Hello World!' # String
var foo = true # Boolean
var foo = 42 # int 42
int foo = 4.2 # exception: invalid double assigned to int
var foo = null # exception: ambiguous data type
null foo = null # exception: null is not a valid data type
Below is a conversion chart with examples. Invalid casting results in nulls for int
, double
, string
, and bool
.
Data Type | Code | Result | Notes |
---|---|---|---|
string->int | int('4') |
4 | |
double->int | int(4.2) |
4 | Math.Round() |
bool->int | int(true) |
1 | true = 1, false = 0 |
int->double | double(4) |
4.0 | |
string->double | double('4') |
4.0 | |
bool->double | double(true) |
1.0 | |
int->string | string(4) |
'4' | |
double->string | string(4.00) |
'4.0' | |
bool->string | string(true) |
'true' | |
int->double | double(4) |
4.0 | |
string->double | double('4') |
4.0 | |
bool->double | double(true) |
1.0 | |
int->bool | bool(1) |
true | |
string->bool | bool('true') |
true | '1' or '0' is equal to null |
double->bool | bool(1.0) |
true | true = 1.0 false = 0.0 |
For lists casting is very important when assigning empty arrays where the data type can not be assumed from the first index. Casting a lists converts every value.
var foo = int([]) # int list
var foo = int(['1', '2', 'A']) # [1, 2, null]
Casting a regex
data type results in a script exception.