Dodo doc > Macros and Templates > Templates > Type With Attributes


Type With Attributes

This is the simplest way to define a family of types.

To apply it, simply declare a type attribute in the type. It is then possible to create a new type by using the type as prototype and changing this attribute value.

Advantages
The type is completely defined and can be used with the attribute default value
No new syntax, simply reuse the concept of prototype
Inconvenients
Type attributes cannot be types

Example:

class String:
   supportedEncoding String.encoding = utf8
   ...
.
def LatinString = new String(encoding: latin1)

There is a special shorthand syntax that allows the derived type be used in a variable declaration. It is written by enclosing the attributes in angled brackets.

Example:

String(<encoding: latin1>) latinString
Rationale: although it is not a real type template, this is sometimes useful when only a value changes between instances of a family of types. The syntax is unified with other uses of prototypes. The special syntax alleviates the need to come up with a name for the derived type and it is more compact.

^ 5.4. Templates

v 5.4.2. Type With Abstract State