Object-Oriented Programming
This guide introduces the OOP model used in Eiger, including Classes and Dataclasses.
Classes
A class defines a blueprint for objects. It can contain properties and functions (methods), and it supports constructors for initializing instances.
Syntax
class ClassName
let [modifiers] propertyName
~ The `new` function acts as a constructor.
func new(parameters)
~ `this` is a reference to the current class instance.
this.propertyName = parameter
end
end
Example
class Person
let private m_name
let private m_age
let private m_birthYear
~ Constructor
func new(name, age, birthYear)
this.m_name = name
this.m_age = age
this.m_birthYear = birthYear
end
~ Method
func summary()
ret (m_name + " (" + m_age.asString + " years old, born " + m_birthYear.asString + ")")
end
end
~ Creating a class instance from a "blueprint" (class)
let personOne = Person("John", 25, 2000)
~ Calling the summary method in personOne
emitln(personOne.summary())
Features
- Properties are declared with
let
, just like variables - Access Modifiers:
public
(default): Accessible from anywhereprivate
: Accessible only within the class
- Constructor:
- Declared with
func new(params)
- Used to initialize the object
- Declared with
Dataclasses
A dataclass defines a group of static properties. It behaves like a class but does not have a constructor.
Syntax
dataclass ClassName
let [modifiers] propertyName = value
func method(params)
~ ...
end
end
Example
dataclass math
~ Mathematical Constants
let readonly pi = 3.14159265358979323846
let readonly e = 2.718281828459045
~ get the unsigned value of a number
func abs(n)
if n < 0 then
ret -n
else
ret n
end
end
func sin(theta)
~ ...
end
end
emitln(math.abs(-128))
emitln(math.sin(math.pi))
Features
- Static by nature: You do not instantiate a dataclass.
- Access Modifiers:
public
(default): Accessible from anywhereprivate
: Accessible only within the dataclass
new
function is not used.