Types / Classes
This chapter is about custom types written natively in Menter. See Java Types for more information about custom types written in Java.
Understanding self
and super
Before diving into custom types, it's important to understand the use of self
, this
, and super
.
self
andthis
can be used interchangeably and are a reference to the current object.If you are inside an object, inside another object,
super
is a reference to the parent object.
Example
self
super
Note that the elements in an object are evaluated in the order they appear. This means that you can't access a symbol that will be defined later when creating the initial value of a symbol. Since functions are evaluated only when called, this will work with functions.
Types
Types are Menter's class system. Types are defined by a function that creates an object.
This function can then be called using the new
keyword to create a new instance. Calling it without new
will return null
.
The constructing function behaves a little different from normal functions and objects, but the returned object is a regular object that can perform all the same operations as any other object.
$init
constructor
The $init
function is called when the type is created using new
. It is a function that takes no arguments and returns nothing. The arguments passed from the new
call are available everywhere in the type on initialization via the args
symbol.
This function is called at the end, when all other attributes have been evaluated to ensure that you can access all attributes from the type.
$fields
attributes
The $fields
attribute is a list of symbols that serves as a shorthand to defining attributes. Each symbol in the list creates an attribute in the type by checking if there is a constructor argument in args
with a matching name.
Is equal to
Is equal to
$extends
inheritance
Either one or a list of constructor calls can be passed to $extends
to inherit from other types. The inherited types are evaluated in order, meaning that the last type in the list will overwrite any attributes that were defined in the previous types. The super types are also evaluated before the type itself, meaning that you can access attributes from the super types in the attributes and functions.
Example with a single super type:
Example with multiple super types: