Declaring functions

Declaring a function

In Hades, function declarations don't contain the return type of the functions. Neither do the datatypes of the input values have to be defined.

Statically typed parameters

A function which is defined with static types, can only be called with those lines. If a function definition for a function with the types specified in the function call does not exist, execution will fail. The return type of a function is declared with ->. If a function doesn't return anything: ->none (although technically a function never doesn't return anything, a fallback 0 is always returned but none warns you if you use the default 0). Only if a function is fully typed, it can be JITed.

Example

with console from std:io

func add(a int, b int) //can also be written as `func add(a b int)
    console.out("Adds two ints")
    put a + b
end

func add(a string, b string) -> string
    console.out("Concatenates two strings")
    put "{} {}".format(a,b)
end

add(2,2) //Output: Adds two ints
add("Hello","world") //Output: Concatenates two strings
add(1.5,1.5) //Execution fails

Dynamically-typed parameters

This function can be used with every datatype.

Example

Varargs

Varargs allow for method invocation with multiple parameters which are treated as a single parameter array by the function. A function can only have one vararg parameter.

Example

Function guards

With function guards, an initial condition has to be fulfilled for the function to be called. If the condition is not fulfilled, another function (ordered by declaration) with the same name and different, or no, function guard is called.

Example

Function guards by match

In addition to the normal function guards, Hades also offers function guards by object/list match. To use this feature, one must first define the function.

Custom blocks

Custom blocks are syntactic sugar that behave like lambdas but are written in do syntax.

Example

Nested functions

As with normal function declarations, nested functions can either explicitly name a type, or not:

Example

Access modifiers

Functions can have access modifiers. If they do, they follow the same rules as variables. See Non-local Variables.

Fixed function

Fixed functions are like static function in Java or C#. One can only declare fixed functions in classes, because in scripts or mixed files, every function which is outside a class is accessible.

Overriding functions

One can override functions (both built-in, as well as inherited) by appending ! to the func statement. Some internal functions are not overridable because of the way they're implemented. These functions are type, nameof, send and self.

Example

Default values

When a function that has default values is being used, the sequence of the parameters which don't have default values stays the same as an invocation without overriding these defaults.

Example

Last updated