A variable can have a type, be nullable, be dynamic and be an array.
Mutable
Mutable variables are declared var. You can specify the type of a variable by appending it after the variable name.
Example
var a string ="Hello, World!"//Immediate assignmentvar b ="What's up?"//Type 'string' is inferredvar c string //Type 'string' is given, but variable is not assignedvar d //Type is inferred on first usagevar e any //Dynamic variable; can be anything, type can change
Immutable
Immutable variables are declared with let. You can specify the type of a variable by appending it after the variable name.
Example
leta string ="Hello, World!"//Immediate assignmentletb="What's up?"//Type 'string' is inferredletc string //Type 'string' is given, but variable is not assignedletd//Type is inferred on first usage
Dynamic
Dynamic variables can change their type at runtime. A dynamic variable is declared by appending *anyafter the variable name. An immutable variable can not be dynamic.
Example
Nullable
You can make variables of simple datatypes nullable by appending ? after the datatype. An immutable variable can not be nullable. Dynamic variables can also not be nullable as assigning null to a dynamic variable works out of the box (it will make the variable an object).
Deconstruct assign
One can deconstruct/match an object or a list and assign it to a variable.
Access modifiers
Access modifiers can control who has access to a specific variable.
Non-local variables
In a class
Variables in a class can have any access modifier. Since Hades has inheritance, variables in classes can also be protected.
Example
In a struct
Variables in a struct can not have an access modifier because in a struct, all variables are publicly accessible. Other than that, variable declaration in a struct is the same as anywhere else.
In a script
Variables in a script can be private or public since scripts can be used by other Hades code. Variables in scripts can not be protected as scripts are unable to inherit.
Access modifier annotate blocks
The access modifier annotate block can set the access specifier for multiple variables at a time. Access modifier blocks can only be used in classes and scripts.
An access modifier block is declared with @modifier. Access modifier blocks follow the same rule as non-local variables.
var MyResult{err := err, result := result} = doSomething()
var MyResult{err: null, result string := result} = doSomething() //throws an exception if err is not null
var {status atom, data} = doSomethingElse()
var {:ok, data} = doSomethingElse() //throws an exception if first element of list is not :ok
protected //Accessible from all inherited members
public //Accessible over direct access
private //Not accessible from outside
class Employee
public let firstname string
public let lastname
private var age int = 18
var attributes string[] //When no access modifier is given, the variable will have private access
end
class Vehicle
@public
var make string
var speed int
end
@protected
var manned? bool
end
end
@private
var connection object
end