THP Arrays contain an ordered list of values of a unique datatype, that is to say,
you cannot store values of different datatypes inside an Array.
If you need to store values of different types, see Tuples.
THP arrays are 0-indexed.
Signature
typeArray[T]=Map[Int,T]
{"errors":[{"reason":"No statement matched","help":null,"start_position":5,"end_position":10,"labels":[{"message":{"dynamic":"This token `Datatype` didnt match any construct"},"start":5,"end":10}]}],"tokens":[{"value":"type","token_type":"Identifier","start_pos":0},{"value":"Array","token_type":"Datatype","start_pos":5},{"value":"[","token_type":"LeftBracket","start_pos":10},{"value":"T","token_type":"Datatype","start_pos":11},{"value":"]","token_type":"RightBracket","start_pos":12},{"value":"=","token_type":"Operator","start_pos":14},{"value":"Map","token_type":"Datatype","start_pos":16},{"value":"[","token_type":"LeftBracket","start_pos":19},{"value":"Int","token_type":"Datatype","start_pos":20},{"value":",","token_type":"Comma","start_pos":23},{"value":"T","token_type":"Datatype","start_pos":25},{"value":"]","token_type":"RightBracket","start_pos":26},{"value":"\n","token_type":"Newline","start_pos":27}],"references":[{"symbol_name":"print","t":"Function","start":0,"end":1},{"symbol_name":"prints","t":"Function","start":0,"end":1}]}
Where T is the datatype that the Array stores. For example:
Array[Int]// An array of integersArray[Float]// An array of floatsArray[Array[String]]// A 2-dimensional array of strings
{"errors":[{"reason":"No statement matched","help":null,"start_position":0,"end_position":5,"labels":[{"message":{"dynamic":"This token `Datatype` didnt match any construct"},"start":0,"end":5}]}],"tokens":[{"value":"Array","token_type":"Datatype","start_pos":0},{"value":"[","token_type":"LeftBracket","start_pos":5},{"value":"Int","token_type":"Datatype","start_pos":6},{"value":"]","token_type":"RightBracket","start_pos":9},{"value":"// An array of integers","token_type":"Comment","start_pos":24},{"value":"\n","token_type":"Newline","start_pos":47},{"value":"Array","token_type":"Datatype","start_pos":48},{"value":"[","token_type":"LeftBracket","start_pos":53},{"value":"Float","token_type":"Datatype","start_pos":54},{"value":"]","token_type":"RightBracket","start_pos":59},{"value":"// An array of floats","token_type":"Comment","start_pos":72},{"value":"\n","token_type":"Newline","start_pos":93},{"value":"Array","token_type":"Datatype","start_pos":94},{"value":"[","token_type":"LeftBracket","start_pos":99},{"value":"Array","token_type":"Datatype","start_pos":100},{"value":"[","token_type":"LeftBracket","start_pos":105},{"value":"String","token_type":"Datatype","start_pos":106},{"value":"]","token_type":"RightBracket","start_pos":112},{"value":"]","token_type":"RightBracket","start_pos":113},{"value":"// A 2-dimensional array of strings","token_type":"Comment","start_pos":118},{"value":"\n","token_type":"Newline","start_pos":153}],"references":[{"symbol_name":"print","t":"Function","start":0,"end":1},{"symbol_name":"prints","t":"Function","start":0,"end":1}]}
PHP array internals
Since THP compiles down to PHP, it's important to understand how PHP
represents arrays internally.
PHP doesn't have arrays. Instead, PHP has ordered maps and syntax sugar
to make them look like arrays.
When declaring an array like:
varletters=["a","b","c"]
{"errors":[{"reason":"Invalid variable declaration","help":null,"start_position":14,"end_position":15,"labels":[{"message":{"dynamic":"Expected an expression here, found a LeftBracket"},"start":14,"end":15}]}],"tokens":[{"value":"var","token_type":"K_Var","start_pos":0},{"value":"letters","token_type":"Identifier","start_pos":4},{"value":"=","token_type":"Operator","start_pos":12},{"value":"[","token_type":"LeftBracket","start_pos":14},{"value":"\"a\"","token_type":"String","start_pos":15},{"value":",","token_type":"Comma","start_pos":18},{"value":"\"b\"","token_type":"String","start_pos":20},{"value":",","token_type":"Comma","start_pos":23},{"value":"\"c\"","token_type":"String","start_pos":25},{"value":"]","token_type":"RightBracket","start_pos":28},{"value":"\n","token_type":"Newline","start_pos":29}],"references":[{"symbol_name":"print","t":"Function","start":0,"end":1},{"symbol_name":"prints","t":"Function","start":0,"end":1}]}
in reality what goes into memory is a map with numbers as keys:
array(
0 => "a",
1 => "b",
2 => "c",
);
As long as this map contains valid keys, it can be treated as an array.
We can loop over it, read and write.
However, once we assign to an invalid index, it cannot be treated as an array anymore.
Invalid indexes are:
Negative numbers
Numbers greater than the array's length.
Problems with iterations
PHP maps preserve insertion order. This means that when iterating over
a PHP map, values are processed in FIFO.
varletters=["a","b","c"]letters[-10]="?"// Assignment to a negative index// Now the array will be:// array(// 0 => "a",// 1 => "b",// 2 => "c",// -10 => "?",// )letters[7]="!"// Out of bounds assignment// Now the array will be:// array(// 0 => "a",// 1 => "b",// 2 => "c",// -10 => "?",// 7 => "!",// )// In this loop, values will be printed based on when// they were inserted, not by order of the index.for#(_,value)inletters{print("{value} ")// outputs: a b c ? !}letters[-4]="???"// array(// 0 => "a",// 1 => "b",// 2 => "c",// -10 => "?",// 7 => "!",// -4 => "???",// )// When pushing, the value will be assigned to the highest key + 1letters.push("/")// This will be at position 8// array(// 0 => "a",// 1 => "b",// 2 => "c",// -10 => "?",// 7 => "!",// -4 => "???",// 8 => "/",// )
{"errors":[{"reason":"Invalid variable declaration","help":null,"start_position":14,"end_position":15,"labels":[{"message":{"dynamic":"Expected an expression here, found a LeftBracket"},"start":14,"end":15}]}],"tokens":[{"value":"var","token_type":"K_Var","start_pos":0},{"value":"letters","token_type":"Identifier","start_pos":4},{"value":"=","token_type":"Operator","start_pos":12},{"value":"[","token_type":"LeftBracket","start_pos":14},{"value":"\"a\"","token_type":"String","start_pos":15},{"value":",","token_type":"Comma","start_pos":18},{"value":"\"b\"","token_type":"String","start_pos":20},{"value":",","token_type":"Comma","start_pos":23},{"value":"\"c\"","token_type":"String","start_pos":25},{"value":"]","token_type":"RightBracket","start_pos":28},{"value":"\n","token_type":"Newline","start_pos":29},{"value":"letters","token_type":"Identifier","start_pos":30},{"value":"[","token_type":"LeftBracket","start_pos":37},{"value":"-","token_type":"Operator","start_pos":38},{"value":"10","token_type":"Int","start_pos":39},{"value":"]","token_type":"RightBracket","start_pos":41},{"value":"=","token_type":"Operator","start_pos":43},{"value":"\"?\"","token_type":"String","start_pos":45},{"value":"// Assignment to a negative index","token_type":"Comment","start_pos":52},{"value":"\n","token_type":"Newline","start_pos":85},{"value":"\n","token_type":"Newline","start_pos":86},{"value":"// Now the array will be:","token_type":"Comment","start_pos":87},{"value":"\n","token_type":"Newline","start_pos":112},{"value":"// array(","token_type":"Comment","start_pos":113},{"value":"\n","token_type":"Newline","start_pos":122},{"value":"// 0 => \"a\",","token_type":"Comment","start_pos":123},{"value":"\n","token_type":"Newline","start_pos":139},{"value":"// 1 => \"b\",","token_type":"Comment","start_pos":140},{"value":"\n","token_type":"Newline","start_pos":156},{"value":"// 2 => \"c\",","token_type":"Comment","start_pos":157},{"value":"\n","token_type":"Newline","start_pos":173},{"value":"// -10 => \"?\",","token_type":"Comment","start_pos":174},{"value":"\n","token_type":"Newline","start_pos":190},{"value":"// )","token_type":"Comment","start_pos":191},{"value":"\n","token_type":"Newline","start_pos":195},{"value":"\n","token_type":"Newline","start_pos":196},{"value":"letters","token_type":"Identifier","start_pos":197},{"value":"[","token_type":"LeftBracket","start_pos":204},{"value":"7","token_type":"Int","start_pos":205},{"value":"]","token_type":"RightBracket","start_pos":206},{"value":"=","token_type":"Operator","start_pos":208},{"value":"\"!\"","token_type":"String","start_pos":210},{"value":"// Out of bounds assignment","token_type":"Comment","start_pos":214},{"value":"\n","token_type":"Newline","start_pos":241},{"value":"\n","token_type":"Newline","start_pos":242},{"value":"// Now the array will be:","token_type":"Comment","start_pos":243},{"value":"\n","token_type":"Newline","start_pos":268},{"value":"// array(","token_type":"Comment","start_pos":269},{"value":"\n","token_type":"Newline","start_pos":278},{"value":"// 0 => \"a\",","token_type":"Comment","start_pos":279},{"value":"\n","token_type":"Newline","start_pos":293},{"value":"// 1 => \"b\",","token_type":"Comment","start_pos":294},{"value":"\n","token_type":"Newline","start_pos":308},{"value":"// 2 => \"c\",","token_type":"Comment","start_pos":309},{"value":"\n","token_type":"Newline","start_pos":323},{"value":"// -10 => \"?\",","token_type":"Comment","start_pos":324},{"value":"\n","token_type":"Newline","start_pos":338},{"value":"// 7 => \"!\",","token_type":"Comment","start_pos":339},{"value":"\n","token_type":"Newline","start_pos":353},{"value":"// )","token_type":"Comment","start_pos":354},{"value":"\n","token_type":"Newline","start_pos":358},{"value":"\n","token_type":"Newline","start_pos":359},{"value":"// In this loop, values will be printed based on when","token_type":"Comment","start_pos":360},{"value":"\n","token_type":"Newline","start_pos":413},{"value":"// they were inserted, not by order of the index.","token_type":"Comment","start_pos":414},{"value":"\n","token_type":"Newline","start_pos":463},{"value":"for","token_type":"Identifier","start_pos":464},{"value":"#","token_type":"Operator","start_pos":468},{"value":"(","token_type":"LeftParen","start_pos":469},{"value":"_","token_type":"Identifier","start_pos":470},{"value":",","token_type":"Comma","start_pos":471},{"value":"value","token_type":"Identifier","start_pos":473},{"value":")","token_type":"RightParen","start_pos":478},{"value":"in","token_type":"Identifier","start_pos":480},{"value":"letters","token_type":"Identifier","start_pos":483},{"value":"{","token_type":"LeftBrace","start_pos":491},{"value":"\n","token_type":"Newline","start_pos":492},{"value":"print","token_type":"Identifier","start_pos":497},{"value":"(","token_type":"LeftParen","start_pos":502},{"value":"\"{value} \"","token_type":"String","start_pos":503},{"value":")","token_type":"RightParen","start_pos":513},{"value":"\n","token_type":"Newline","start_pos":514},{"value":"// outputs: a b c ? !","token_type":"Comment","start_pos":519},{"value":"\n","token_type":"Newline","start_pos":540},{"value":"}","token_type":"RightBrace","start_pos":541},{"value":"\n","token_type":"Newline","start_pos":542},{"value":"\n","token_type":"Newline","start_pos":543},{"value":"letters","token_type":"Identifier","start_pos":544},{"value":"[","token_type":"LeftBracket","start_pos":551},{"value":"-","token_type":"Operator","start_pos":552},{"value":"4","token_type":"Int","start_pos":553},{"value":"]","token_type":"RightBracket","start_pos":554},{"value":"=","token_type":"Operator","start_pos":556},{"value":"\"???\"","token_type":"String","start_pos":558},{"value":"\n","token_type":"Newline","start_pos":563},{"value":"// array(","token_type":"Comment","start_pos":564},{"value":"\n","token_type":"Newline","start_pos":573},{"value":"// 0 => \"a\",","token_type":"Comment","start_pos":574},{"value":"\n","token_type":"Newline","start_pos":588},{"value":"// 1 => \"b\",","token_type":"Comment","start_pos":589},{"value":"\n","token_type":"Newline","start_pos":603},{"value":"// 2 => \"c\",","token_type":"Comment","start_pos":604},{"value":"\n","token_type":"Newline","start_pos":618},{"value":"// -10 => \"?\",","token_type":"Comment","start_pos":619},{"value":"\n","token_type":"Newline","start_pos":633},{"value":"// 7 => \"!\",","token_type":"Comment","start_pos":634},{"value":"\n","token_type":"Newline","start_pos":648},{"value":"// -4 => \"???\",","token_type":"Comment","start_pos":649},{"value":"\n","token_type":"Newline","start_pos":665},{"value":"// )","token_type":"Comment","start_pos":666},{"value":"\n","token_type":"Newline","start_pos":670},{"value":"\n","token_type":"Newline","start_pos":671},{"value":"// When pushing, the value will be assigned to the highest key + 1","token_type":"Comment","start_pos":672},{"value":"\n","token_type":"Newline","start_pos":738},{"value":"letters","token_type":"Identifier","start_pos":739},{"value":".","token_type":"Operator","start_pos":746},{"value":"push","token_type":"Identifier","start_pos":747},{"value":"(","token_type":"LeftParen","start_pos":751},{"value":"\"/\"","token_type":"String","start_pos":752},{"value":")","token_type":"RightParen","start_pos":755},{"value":"// This will be at position 8","token_type":"Comment","start_pos":757},{"value":"\n","token_type":"Newline","start_pos":786},{"value":"\n","token_type":"Newline","start_pos":787},{"value":"// array(","token_type":"Comment","start_pos":788},{"value":"\n","token_type":"Newline","start_pos":797},{"value":"// 0 => \"a\",","token_type":"Comment","start_pos":798},{"value":"\n","token_type":"Newline","start_pos":812},{"value":"// 1 => \"b\",","token_type":"Comment","start_pos":813},{"value":"\n","token_type":"Newline","start_pos":827},{"value":"// 2 => \"c\",","token_type":"Comment","start_pos":828},{"value":"\n","token_type":"Newline","start_pos":842},{"value":"// -10 => \"?\",","token_type":"Comment","start_pos":843},{"value":"\n","token_type":"Newline","start_pos":857},{"value":"// 7 => \"!\",","token_type":"Comment","start_pos":858},{"value":"\n","token_type":"Newline","start_pos":872},{"value":"// -4 => \"???\",","token_type":"Comment","start_pos":873},{"value":"\n","token_type":"Newline","start_pos":889},{"value":"// 8 => \"/\",","token_type":"Comment","start_pos":890},{"value":"\n","token_type":"Newline","start_pos":904},{"value":"// )","token_type":"Comment","start_pos":905},{"value":"\n","token_type":"Newline","start_pos":909}],"references":[{"symbol_name":"print","t":"Function","start":0,"end":1},{"symbol_name":"prints","t":"Function","start":0,"end":1}]}
This is one of many fundamental flaws with PHP. The only way to solve it
would be to check every insertion at runtime, and this would have a
performance penalty.
From now on, the documentation will continue to work with the Array
abstraction, as if all indexes were valid.
Usage
Empty array
To create an empty array use square brackets.
If you create an empty array, you must specify the datatype.
Array[Int]empty=[]
{"errors":[{"reason":"No statement matched","help":null,"start_position":0,"end_position":5,"labels":[{"message":{"dynamic":"This token `Datatype` didnt match any construct"},"start":0,"end":5}]}],"tokens":[{"value":"Array","token_type":"Datatype","start_pos":0},{"value":"[","token_type":"LeftBracket","start_pos":5},{"value":"Int","token_type":"Datatype","start_pos":6},{"value":"]","token_type":"RightBracket","start_pos":9},{"value":"empty","token_type":"Identifier","start_pos":11},{"value":"=","token_type":"Operator","start_pos":17},{"value":"[","token_type":"LeftBracket","start_pos":19},{"value":"]","token_type":"RightBracket","start_pos":20},{"value":"\n","token_type":"Newline","start_pos":21}],"references":[{"symbol_name":"print","t":"Function","start":0,"end":1},{"symbol_name":"prints","t":"Function","start":0,"end":1}]}
Array with items
To create an array with items use square brackets notation:
valnumbers=[0,1,2,3,4,5]
{"errors":[{"reason":"Invalid variable declaration","help":null,"start_position":14,"end_position":15,"labels":[{"message":{"dynamic":"Expected an expression here, found a LeftBracket"},"start":14,"end":15}]}],"tokens":[{"value":"val","token_type":"K_Val","start_pos":0},{"value":"numbers","token_type":"Identifier","start_pos":4},{"value":"=","token_type":"Operator","start_pos":12},{"value":"[","token_type":"LeftBracket","start_pos":14},{"value":"0","token_type":"Int","start_pos":15},{"value":",","token_type":"Comma","start_pos":16},{"value":"1","token_type":"Int","start_pos":18},{"value":",","token_type":"Comma","start_pos":19},{"value":"2","token_type":"Int","start_pos":21},{"value":",","token_type":"Comma","start_pos":22},{"value":"3","token_type":"Int","start_pos":24},{"value":",","token_type":"Comma","start_pos":25},{"value":"4","token_type":"Int","start_pos":27},{"value":",","token_type":"Comma","start_pos":28},{"value":"5","token_type":"Int","start_pos":30},{"value":"]","token_type":"RightBracket","start_pos":31},{"value":"\n","token_type":"Newline","start_pos":32}],"references":[{"symbol_name":"print","t":"Function","start":0,"end":1},{"symbol_name":"prints","t":"Function","start":0,"end":1}]}
When the array is not empty, you don't need to specify a datatype.
Assignment to elements
Use square brackets notation to insert into an array or modify it:
To modify an array it must be mutable, that is, assigned to a var
instead of a val.
// This array cannot be modified, as it's declared with \`val\`valimmutable=[1,2,3]// This is a compile time errorimmutable[0]=322// This array can be modified, as it's declared with \`var\`varmutable=[1,2,3]// Okmutable[0]=322
{"errors":[{"reason":"Invalid variable declaration","help":null,"start_position":80,"end_position":81,"labels":[{"message":{"dynamic":"Expected an expression here, found a LeftBracket"},"start":80,"end":81}]}],"tokens":[{"value":"// This array cannot be modified, as it's declared with \\`val\\`","token_type":"Comment","start_pos":0},{"value":"\n","token_type":"Newline","start_pos":63},{"value":"val","token_type":"K_Val","start_pos":64},{"value":"immutable","token_type":"Identifier","start_pos":68},{"value":"=","token_type":"Operator","start_pos":78},{"value":"[","token_type":"LeftBracket","start_pos":80},{"value":"1","token_type":"Int","start_pos":81},{"value":",","token_type":"Comma","start_pos":82},{"value":"2","token_type":"Int","start_pos":84},{"value":",","token_type":"Comma","start_pos":85},{"value":"3","token_type":"Int","start_pos":87},{"value":"]","token_type":"RightBracket","start_pos":88},{"value":"\n","token_type":"Newline","start_pos":89},{"value":"// This is a compile time error","token_type":"Comment","start_pos":90},{"value":"\n","token_type":"Newline","start_pos":121},{"value":"immutable","token_type":"Identifier","start_pos":122},{"value":"[","token_type":"LeftBracket","start_pos":131},{"value":"0","token_type":"Int","start_pos":132},{"value":"]","token_type":"RightBracket","start_pos":133},{"value":"=","token_type":"Operator","start_pos":135},{"value":"322","token_type":"Int","start_pos":137},{"value":"\n","token_type":"Newline","start_pos":140},{"value":"\n","token_type":"Newline","start_pos":141},{"value":"// This array can be modified, as it's declared with \\`var\\`","token_type":"Comment","start_pos":142},{"value":"\n","token_type":"Newline","start_pos":202},{"value":"var","token_type":"K_Var","start_pos":203},{"value":"mutable","token_type":"Identifier","start_pos":207},{"value":"=","token_type":"Operator","start_pos":215},{"value":"[","token_type":"LeftBracket","start_pos":217},{"value":"1","token_type":"Int","start_pos":218},{"value":",","token_type":"Comma","start_pos":219},{"value":"2","token_type":"Int","start_pos":221},{"value":",","token_type":"Comma","start_pos":222},{"value":"3","token_type":"Int","start_pos":224},{"value":"]","token_type":"RightBracket","start_pos":225},{"value":"\n","token_type":"Newline","start_pos":226},{"value":"// Ok","token_type":"Comment","start_pos":227},{"value":"\n","token_type":"Newline","start_pos":232},{"value":"mutable","token_type":"Identifier","start_pos":233},{"value":"[","token_type":"LeftBracket","start_pos":240},{"value":"0","token_type":"Int","start_pos":241},{"value":"]","token_type":"RightBracket","start_pos":242},{"value":"=","token_type":"Operator","start_pos":244},{"value":"322","token_type":"Int","start_pos":246},{"value":"\n","token_type":"Newline","start_pos":249}],"references":[{"symbol_name":"print","t":"Function","start":0,"end":1},{"symbol_name":"prints","t":"Function","start":0,"end":1}]}
To append an element to an array, use the method push():
mutable.push(4)
{"errors":[{"reason":"No statement matched","help":null,"start_position":7,"end_position":8,"labels":[{"message":{"dynamic":"This token `Operator` didnt match any construct"},"start":7,"end":8}]}],"tokens":[{"value":"mutable","token_type":"Identifier","start_pos":0},{"value":".","token_type":"Operator","start_pos":7},{"value":"push","token_type":"Identifier","start_pos":8},{"value":"(","token_type":"LeftParen","start_pos":12},{"value":"4","token_type":"Int","start_pos":13},{"value":")","token_type":"RightParen","start_pos":14},{"value":"\n","token_type":"Newline","start_pos":15}],"references":[{"symbol_name":"print","t":"Function","start":0,"end":1},{"symbol_name":"prints","t":"Function","start":0,"end":1}]}
{"errors":[{"reason":"Invalid variable declaration","help":null,"start_position":16,"end_position":17,"labels":[{"message":{"dynamic":"Expected an expression here, found a LeftBracket"},"start":16,"end":17}]}],"tokens":[{"value":"val","token_type":"K_Val","start_pos":0},{"value":"my_colors","token_type":"Identifier","start_pos":4},{"value":"=","token_type":"Operator","start_pos":14},{"value":"[","token_type":"LeftBracket","start_pos":16},{"value":"\"red\"","token_type":"String","start_pos":17},{"value":",","token_type":"Comma","start_pos":22},{"value":"\"green\"","token_type":"String","start_pos":24},{"value":",","token_type":"Comma","start_pos":31},{"value":"\"blue\"","token_type":"String","start_pos":33},{"value":"]","token_type":"RightBracket","start_pos":39},{"value":"\n","token_type":"Newline","start_pos":40},{"value":"\n","token_type":"Newline","start_pos":41},{"value":"for","token_type":"Identifier","start_pos":42},{"value":"#","token_type":"Operator","start_pos":46},{"value":"(","token_type":"LeftParen","start_pos":47},{"value":"idx","token_type":"Identifier","start_pos":48},{"value":",","token_type":"Comma","start_pos":51},{"value":"color","token_type":"Identifier","start_pos":53},{"value":")","token_type":"RightParen","start_pos":58},{"value":"in","token_type":"Identifier","start_pos":60},{"value":"my_colors","token_type":"Identifier","start_pos":63},{"value":"{","token_type":"LeftBrace","start_pos":73},{"value":"\n","token_type":"Newline","start_pos":74},{"value":"print","token_type":"Identifier","start_pos":79},{"value":"(","token_type":"LeftParen","start_pos":84},{"value":"\"{color} \"","token_type":"String","start_pos":85},{"value":")","token_type":"RightParen","start_pos":95},{"value":"\n","token_type":"Newline","start_pos":96},{"value":"}","token_type":"RightBrace","start_pos":97},{"value":"\n","token_type":"Newline","start_pos":98}],"references":[{"symbol_name":"print","t":"Function","start":0,"end":1},{"symbol_name":"prints","t":"Function","start":0,"end":1}]}
red green blue
A for loop automatically declares new immutable variables. It is a compile
error to attempt to modify those, as in the following snippet:
valmy_colors=["red","green","blue"]for#(_,c)inmy_colors{c="orange"// Compile error: Can't assign to an immutable variableprint("{c} ")}
{"errors":[{"reason":"Invalid variable declaration","help":null,"start_position":16,"end_position":17,"labels":[{"message":{"dynamic":"Expected an expression here, found a LeftBracket"},"start":16,"end":17}]}],"tokens":[{"value":"val","token_type":"K_Val","start_pos":0},{"value":"my_colors","token_type":"Identifier","start_pos":4},{"value":"=","token_type":"Operator","start_pos":14},{"value":"[","token_type":"LeftBracket","start_pos":16},{"value":"\"red\"","token_type":"String","start_pos":17},{"value":",","token_type":"Comma","start_pos":22},{"value":"\"green\"","token_type":"String","start_pos":24},{"value":",","token_type":"Comma","start_pos":31},{"value":"\"blue\"","token_type":"String","start_pos":33},{"value":"]","token_type":"RightBracket","start_pos":39},{"value":"\n","token_type":"Newline","start_pos":40},{"value":"\n","token_type":"Newline","start_pos":41},{"value":"for","token_type":"Identifier","start_pos":42},{"value":"#","token_type":"Operator","start_pos":46},{"value":"(","token_type":"LeftParen","start_pos":47},{"value":"_","token_type":"Identifier","start_pos":48},{"value":",","token_type":"Comma","start_pos":49},{"value":"c","token_type":"Identifier","start_pos":51},{"value":")","token_type":"RightParen","start_pos":52},{"value":"in","token_type":"Identifier","start_pos":54},{"value":"my_colors","token_type":"Identifier","start_pos":57},{"value":"{","token_type":"LeftBrace","start_pos":67},{"value":"\n","token_type":"Newline","start_pos":68},{"value":"c","token_type":"Identifier","start_pos":73},{"value":"=","token_type":"Operator","start_pos":75},{"value":"\"orange\"","token_type":"String","start_pos":77},{"value":"// Compile error: Can't assign to an immutable variable","token_type":"Comment","start_pos":86},{"value":"\n","token_type":"Newline","start_pos":141},{"value":"print","token_type":"Identifier","start_pos":146},{"value":"(","token_type":"LeftParen","start_pos":151},{"value":"\"{c} \"","token_type":"String","start_pos":152},{"value":")","token_type":"RightParen","start_pos":158},{"value":"\n","token_type":"Newline","start_pos":159},{"value":"}","token_type":"RightBrace","start_pos":160},{"value":"\n","token_type":"Newline","start_pos":161}],"references":[{"symbol_name":"print","t":"Function","start":0,"end":1},{"symbol_name":"prints","t":"Function","start":0,"end":1}]}
You can also declare an index along with the value:
{"errors":[{"reason":"Invalid variable declaration","help":null,"start_position":16,"end_position":17,"labels":[{"message":{"dynamic":"Expected an expression here, found a LeftBracket"},"start":16,"end":17}]}],"tokens":[{"value":"val","token_type":"K_Val","start_pos":0},{"value":"my_colors","token_type":"Identifier","start_pos":4},{"value":"=","token_type":"Operator","start_pos":14},{"value":"[","token_type":"LeftBracket","start_pos":16},{"value":"\"red\"","token_type":"String","start_pos":17},{"value":",","token_type":"Comma","start_pos":22},{"value":"\"green\"","token_type":"String","start_pos":24},{"value":",","token_type":"Comma","start_pos":31},{"value":"\"blue\"","token_type":"String","start_pos":33},{"value":"]","token_type":"RightBracket","start_pos":39},{"value":"\n","token_type":"Newline","start_pos":40},{"value":"\n","token_type":"Newline","start_pos":41},{"value":"for","token_type":"Identifier","start_pos":42},{"value":"#","token_type":"Operator","start_pos":46},{"value":"(","token_type":"LeftParen","start_pos":47},{"value":"index","token_type":"Identifier","start_pos":48},{"value":",","token_type":"Comma","start_pos":53},{"value":"color","token_type":"Identifier","start_pos":55},{"value":")","token_type":"RightParen","start_pos":60},{"value":"in","token_type":"Identifier","start_pos":62},{"value":"my_colors","token_type":"Identifier","start_pos":65},{"value":"{","token_type":"LeftBrace","start_pos":75},{"value":"\n","token_type":"Newline","start_pos":76},{"value":"println","token_type":"Identifier","start_pos":81},{"value":"(","token_type":"LeftParen","start_pos":88},{"value":"\"item {index}: {c}\"","token_type":"String","start_pos":89},{"value":")","token_type":"RightParen","start_pos":108},{"value":"\n","token_type":"Newline","start_pos":109},{"value":"}","token_type":"RightBrace","start_pos":110},{"value":"\n","token_type":"Newline","start_pos":111}],"references":[{"symbol_name":"print","t":"Function","start":0,"end":1},{"symbol_name":"prints","t":"Function","start":0,"end":1}]}
item 0: red
item 1: green
item 2: blue
Access
To access a value of the array use square brackets notation:
print(colors[0])
{"errors":[{"reason":"Expected ')' after function call","help":null,"start_position":12,"end_position":13,"labels":[]}],"tokens":[{"value":"print","token_type":"Identifier","start_pos":0},{"value":"(","token_type":"LeftParen","start_pos":5},{"value":"colors","token_type":"Identifier","start_pos":6},{"value":"[","token_type":"LeftBracket","start_pos":12},{"value":"0","token_type":"Int","start_pos":13},{"value":"]","token_type":"RightBracket","start_pos":14},{"value":")","token_type":"RightParen","start_pos":15},{"value":"\n","token_type":"Newline","start_pos":16}],"references":[{"symbol_name":"print","t":"Function","start":0,"end":1},{"symbol_name":"prints","t":"Function","start":0,"end":1}]}
Destructuring
THP arrays don't have destructuring, since the values can all be null.
If you know that the number of elements is fixed and valid, use Tuples instead.
Operators
While PHP allows using certain operators with arrays, THP disallows that.
Methods that perform comparisons should be used instead.
Assignment
// TODO: Detail that assignment of arrays is copy on write
Methods
In the parameters, self is the array to operate on.