Dodo doc > Types > Nested Types


Nested Types

A type declaration can be nested in another type. In that case the nested type is linked to the enclosing type instance which creates it.

The nested type gets a default value only if it has no constructors and no attribute which value depends on the enclosing type instance. If the nested type has constructors, including a constructor with no arguments, it does not have a default value. This is because the constructor needs to know the enclosing object before it can run.

In the nested type functions, declarations of the enclosing type are declared. But the "self" variable is a reference to the current nested type instance.

Examples:

class House
{
   struct Window(Integer)
}

class FoodProcessor:
def SpeedSetting = new Integer
def speed -> SpeedSetting # uses a nested type
.
Rationale: setting a default value to the types is desirable. However if the no-argument constructor of nested types was invoked at instance creation, it would make it more complex and slow and could add side effects to an operation which is normally stateless (prototype cloning)

A nested type can be overridden. Declarations in an overriding nested type declaration can override existing declarations in the same way as other types. Constructors are inherited and can also be overridden. Any added attribute keeps its default value when an inherited constructor is called.

Example:

struct ^Window:
   make ^Self(Integer)
   make Self(WindowFinish)
.
Rationale: A method is a kind of nested type and it is expected that it can be overridden. That also extends to other nested types. Constructors have to produce a fully initialised object so inherited constructors can only rely on default values for the added attributes.

^ 4.3. Use as Prototype

v 4.5. Type Members