Declaring classes

Instantiating classes

with Calculator as calc from calc //loads from calc.hd

var calculator = calc() //no new keyword in Hades; instead the proto 'calc' is called

Declaring a class

Example

with date from std:date

class Member
    private var id string
    
    @public
        var firstname
        var lastname
        var birthday
    end
    
    func Member(firstname, lastname, birthday, id)
        this.firstname = firstname
        this.lastname = lastname
        this.birthday = birthday
        this.id = id
    end
end

var member1 = Member("John", "Doe", Date(1,1,1970), "25aca5a7-cbfa-47ed-aeb5-f96cb1eb46ee")
//Create a new member instance

Declaring a class without a constructor

A class without a constructor can still be instantiated. In the following example the class Calculator has two fixed methods which can be accessed without the class needing to be instantiated.

Example

Declaring a non-instantiable class

A class marked fixed can not be instantiated. All functions or structs declared in it, are fixed. It may contain a constructor (but it can't be accessed without using reflection). One can declare a non-fixed class inside a fixed class and vice-versa.

Example

Declaring a class within a class

Example

Declaring a class within a function

In Hades, it is possible to declare a class in a function. This function can only be instantiated (provided it's an instantiable class) in said function but can be returned and therefore used outside the function scope.

Example

Working with inheritance

Base classes

Simple inheritance

Example

Multiple inheritance

When functions overlap each other when inheriting from multiple members, the order of the members to inherit from dictates which function is taken.

Example

Super

If you still want to make the code from the example above work, you need to call the method on Mother explicitly. The built-in super function allows you to do exactly that.

Overriding inherited members

To override a function, use the func! keyword. This overrides functions and functions groups with function guards.

Example

Last updated