Template Types
Type templates with promised functions that multiple real types can use.
This is an advanced feature.
Template types can be used to define common elements that multiple types will use. They can also be used to create type hierarchies.
All objects have one concrete (real) type. An object may have multiple template types.

Template Type Structure

Template types have a single name input in their member pattern. The type member goes at the root level of the script (with no indent.)
1
template type %TypeName%:
2
Copied!
This name is written in standard, a-z0-9_ format, without any quotes or brackets.
1
template type Thing:
2
Copied!
For clarity, example type names are written in UpperCamelCase but this is only a style choice to prevent confusion with functions.
A template type can hold function members inside it, which need to be indented by an extra unit.
Functions inside the type are considered to belong to that type. These are different from regular functions, but the format for writing them is the same.
1
template type Thing:
2
function my_func:
3
trigger:
4
print "hello"
Copied!
Unlike regular type functions, template functions do not need to have a trigger.
1
template type Thing:
2
function my_func:
3
function another_func:
Copied!
An empty template function cannot be directly run.
These functions may still have return types and parameters.
1
template type NamedThing:
2
function set_name (name):
3
function get_name:
4
return: String
Copied!
Template types may use other templates.
1
template type Shape:
2
function get_sides:
3
​
4
template type Quadrilateral:
5
template: Shape
6
function get_sides:
7
trigger:
8
return 4
Copied!

Using Template Types

A template has two core uses.

1. Categorising and Identifying Objects

Template types can form a hierarchy structure.
1
template type Shape:
2
function get_sides:
3
​
4
template type Quadrilateral:
5
template: Shape
6
function get_sides:
7
trigger:
8
return 4
9
​
10
type Square:
11
template: Quadrilateral
Copied!
This can be used to provide information about objects of these types.
1
set {var} to a new Square
2
assert {var} is a Quadrilateral // Square uses Quadrilateral
3
assert {var} is a Shape // Quadrilateral uses Shape
4
assert {var} is an Object // all types are Objects
5
​
6
if {var} is a Shape:
7
print get_sides() from {var}
Copied!

2. Avoid Repeating Code

When a type uses a template, it inherits all of its functions. This allows multiple different types to use the same code, without needing to copy it.
1
template type Quadrilateral:
2
template: Shape
3
function get_sides:
4
trigger:
5
return 4
6
​
7
type Square:
8
template: Quadrilateral
9
​
10
type Rectangle:
11
template: Quadrilateral
Copied!
In this example structure, both Square and Rectangle will inherit the get_sides() function from their shared template Quadrilateral.
1
set {square} to a new Square
2
set {rectangle} to a new Rectangle
3
​
4
assert get_sides() from {square} is 4
5
assert get_sides() from {rectangle} is 4
Copied!
This default function can be overridden by a type that uses the template, simply by declaring an identical function.
1
template type Shape:
2
function get_sides:
3
trigger:
4
return 2
5
​
6
type Square:
7
template: Shape
8
function get_sides: // overrides Shape's get_sides()
9
trigger:
10
return 4
Copied!
The function must be exactly identical to properly override. This includes specifying the same return-type.
By using template functions with no trigger, more advanced behaviour can be created.
These template functions assure us that a matching function will exist, but provide no behaviour or code for one.
Template functions are 'promised' - they don't exist yet, but promise that the type using this template is going to have that function.
The actual trigger for this template function must be added to the type using this template.
1
template type Shape:
2
function get_sides: // promised function
3
function describe:
4
trigger:
5
set {sides} to get_sides() from this object
6
print "This shape has " + {sides} + " sides!"
7
​
8
type Triangle:
9
function get_sides: // keeps the promise
10
trigger:
11
return 3
Copied!
With this structure, we can now run the describe() function from Shape but it will use the get_sides() trigger from Triangle.
1
set {shape} to a new Triangle
2
run describe() from {shape} // runs Triangle's describe()
Copied!