HadesLang Doc
Search…
Declaring arrays

Declaring one-dimensional arrays

A one dimensional fixed array is created either via an array literal or via appending [] to the type of the variable with the size or *(for an infinite array) between the brackets. If no variable type is given, the type array is infered on first use and the variable will be an infinite array

Example

1
with console from std:io
2
3
var hello = {"Hello","world"} //type of the array is infered here, fooBar is now a string[2]
4
console.out(hello[0] + hello[1]) //Output: Hello world
5
6
var fooBar
7
fooBar[0] = 10 //type of the array is infered here, fooBar is now an int[*]
8
fooBar[1] = 20
9
10
var fib int[10] = {1,1,2,3,5,8,13,21,34,55}
11
12
var barFoo
13
barFoo[0] = null //type of the array is infered here, barFoo is now an object[*]
14
15
let text string[*] = console.in().split(" ")
Copied!

Declaring arrays with mixed types

An array of *any can contain data of multiple types. This means that the array can be used as a tuple.
1
var arr *any[2]
2
arr = {:ok, "Hello world"}
3
4
let result = {1, true, 20.231}
Copied!

Declaring multi-dimensional arrays

The syntax of creating multi-dimensional arrays is similar to that of creating one-dimensional arrays. But instead of a single size, you put the size of each dimension between the brackets seperated by a , .

Example

1
var matrix int[3,3] = {{1,0,0},{0,1,0},{0,0,1}}
2
3
var names = {{"John", "Greg"}, {"Anna", "Susan"}} //type of the array is infered here, names is now a string[2.2]
4
5
let a int[*] = {{3,7,3,0},{0,2,-1,1},{5,4,3,2},{6,6,4,-1}}
Copied!

Arrays of nullable types

Making an array nullable is done in the same way as making a variable nullable: you put a ? in front of the datatype or let the interpreter infer the array type.

Example

1
var 3dArray int?[2,2,2] = {{{1,2},{3,null}},{{null,6},{7,8}}}
2
3
var strings = {null, "Hello","World"} //type of the array is infered here, strings is now a string?[3]
Copied!
Last modified 1yr ago