HadesLang Doc
Search…
Declaring classes

Instantiating classes

1
with Calculator as calc from calc //loads from calc.hd
2
3
var calculator = calc() //no new keyword in Hades; instead the proto 'calc' is called
Copied!

Declaring a class

Example

1
with date from std:date
2
3
class Member
4
private var id string
5
6
@public
7
var firstname
8
var lastname
9
var birthday
10
end
11
12
func Member(firstname, lastname, birthday, id)
13
this.firstname = firstname
14
this.lastname = lastname
15
this.birthday = birthday
16
this.id = id
17
end
18
end
19
20
var member1 = Member("John", "Doe", Date(1,1,1970), "25aca5a7-cbfa-47ed-aeb5-f96cb1eb46ee")
21
//Create a new member instance
Copied!

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

1
class Calculator
2
fixed func add(a,b)
3
put a + b
4
end
5
6
fixed func sub(a,b)
7
put a - b
8
end
9
end
10
11
var calc = Calculator //assign calc to the class proto
12
calc.add(2,2)
Copied!

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

1
fixed class Calculator
2
class Adder
3
func add(a,b)
4
put a + b
5
end
6
end
7
8
func add(a,b)
9
put Adder().add(a,b)
10
end
11
12
func sub(a,b)
13
put a - b
14
end
15
end
16
17
Calculator.add(2,2)
18
Calculator.Adder().add(2,2) //This works too because we declared a non-fixed (instantiable) class inside the fixed class
Copied!

Declaring a class within a class

Example

1
with console fixed from std:io
2
3
class outerClass
4
class innerClass
5
func innerClass()
6
out("Hello from inner class")
7
end
8
end
9
end
10
11
var innerClass = outerClass.innerClass() //Outputs: Hello from inner class
Copied!

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

1
func getPerson(fn,ln)
2
class Person
3
@public
4
var firstname
5
var lastname
6
end
7
8
func Person(firstname,lastname)
9
this.firstname = firstname
10
this.lastname = lastname
11
end
12
end
13
14
put Person(fn,ln)
15
end
Copied!

Working with inheritance

Base classes

1
with console fixed from std:io
2
3
class Mother
4
func talk()
5
out("I am female")
6
end
7
end
8
9
class Father
10
func talk()
11
out("I am male")
12
end
13
end
Copied!

Simple inheritance

Example

1
class Daughter < Mother
2
end
3
4
Daughter().talk() //Outputs: I am female
Copied!

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

1
class Child < Mother, Father
2
end
3
4
Child().talk() //Outputs: I am male
5
/*
6
Because the Father class was inherited after the Mother class,
7
the talk function of Father overwrites the talk function in Mother
8
*/
Copied!

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.
1
Child().super(Mother).talk()
Copied!

Overriding inherited members

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

Example

1
with console fixed from
2
3
class OverrideChild < Father
4
func! talk()
5
out("I am a child")
6
end
7
end
8
9
OverrideChild().talk() //Outputs: I am a child
Copied!
Last modified 1yr ago