HadesLang Doc
  • Overview
  • Getting Started
    • Installing Hades
    • Basic Syntax
    • Coding Conventions
  • Language Spec
    • Foundation
      • Types
      • Built-in functions
    • Operators
      • Comparison and equality
      • Logical operators
      • Bitwise operators
      • Compound Assignment Operators
      • Operator overloading
    • Control flow
      • Conditions
      • Loops
      • Exception handling
      • Ternary and nullcheck operators
      • Pipelines
    • Classes and variables
      • Declaring variables
      • Declaring classes
      • Declaring structs
      • Declaring protos
      • Declaring arrays
      • Type conversions in simple variable types
    • Actors
      • Message passing
      • GenServer
      • Channels
    • Functions and lambdas
      • Declaring functions
      • Declaring lambdas
    • Functions of simple types
      • int
      • string
      • float
      • bool
      • atom
      • pid
    • Other
      • Annotations
      • Comments
      • Preprocessor statements
      • Reflection
      • Script file arguments
      • this keyword
  • Core Libraries
    • Standard library
      • std:io
        • console
        • file
        • directory
      • std:exceptions
      • std:int
      • std:internals
        • annotations
          • findMethod
          • findMethodInProto
          • findMethods
          • findMethodsInProto
        • function
      • std:collections
        • map
        • list
      • std:math
        • math
        • constants
        • matrix
      • std:networking
      • std:os
      • std:params
      • std:string
      • std:sql
    • Extra libraries
      • mssql:client
  • Other
    • Tools
    • Examples
    • Todos
Powered by GitBook
On this page
  • Declaring one-dimensional arrays
  • Example
  • Declaring arrays with mixed types
  • Declaring multi-dimensional arrays
  • Example
  • Arrays of nullable types
  • Example
  1. Language Spec
  2. Classes and variables

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

with console from std:io

var hello = {"Hello","world"} //type of the array is infered here, fooBar is now a string[2]
console.out(hello[0] + hello[1]) //Output: Hello world

var fooBar
fooBar[0] = 10 //type of the array is infered here, fooBar is now an int[*]
fooBar[1] = 20

var fib int[10] = {1,1,2,3,5,8,13,21,34,55}

var barFoo
barFoo[0] = null //type of the array is infered here, barFoo is now an object[*]

let text string[*] = console.in().split(" ")

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.

var arr *any[2]
arr = {:ok, "Hello world"}

let result = {1, true, 20.231}

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

var matrix int[3,3] = {{1,0,0},{0,1,0},{0,0,1}}

var names = {{"John", "Greg"}, {"Anna", "Susan"}} //type of the array is infered here, names is now a string[2.2]

let a int[*] = {{3,7,3,0},{0,2,-1,1},{5,4,3,2},{6,6,4,-1}}

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

var 3dArray int?[2,2,2] = {{{1,2},{3,null}},{{null,6},{7,8}}}

var strings = {null, "Hello","World"} //type of the array is infered here, strings is now a string?[3]
PreviousDeclaring protosNextType conversions in simple variable types

Last updated 5 years ago