Constructors - Manhunter07/MFL GitHub Wiki
Constructors are functions that implicitly carry the name and return type of a given type. They are used to initialize new instances of a type, regardless of its kind.
Together with converters, constructors are among the possible type methods. Constructors support the same features and are declared the same as them. Those are the same as functions with the exception of return types, as the return type of a constructor is implicitly the type it belongs to. If a constructor returns a value that is not supported by the type it is attached to, an exception is raised.
Declaration
A constructor is declared using the constructor keyword, followed by a type identifier and optional parameters, similar as in functions. After that, the function body with an expression returning the newly-created type instance is written:
constructor Vegetable(Name: string, Color: integer) = {Name = Name, Color = Color}
The type has to be declared before the constructor can be declared. A constructor can only be declared inside the same package the type itself was declared in. If a type is declared locally, its constructor is implicitely available only locally as well. A type can only have one declared constructor.
Call
After a constructor has been defined, the type can be instantiated using the constructor function-like:
const Apple = Vegetable("Malus", $ff0000)
In this case, the Apple constant now has the value of {Name = "Malus", Color = $ff0000}.