Types

The following covers the definition of type and their composition and type expressions.

Definition

There are three different kinds of type definitions in TreeLDR:

Empty Type

The empty type is defined by simply declaring the name of the type directly followed by a semicolon.

// Empty type.

type MyType;

Normal Type

A normal type also declares properties that can be applied to the instances of this type. Properties are listed inside the definition between braces:

// Normal type.

type MyType {

property1: Type1,

// ...

propertyN: TypeN

}

The type of each property is specified after a colon *type expression*. If a property listed in the definition is declared outside the type definition, it is not required to specify the type of the property again.

`:`

using a Type Alias

Lastly, it is possible to define type aliases. Such a type is defined using a *type expression*.

type MyType = TypeExpression;

Type Expression

A type expression is a composition of existing types by union, intersection and restriction.

Union

The union between two or more types is expressed using the

`|`

operator.TypeA | ... | TypeN

An instance of this type is also an instance of at least one of the listed sub-types.

Intersection

The intersection between two or more types is expressed using the

`&`

operator.TypeA & ... & TypeN

An instance of this type is also an instance of all the listed sub-types.

Range Restriction

Range restrictions are used to select a subset of a type by restricting the range of one of its properties. There are two kinds of range restrictions: universal and existential.

A universal range restriction is built using the

`all`

keyword:all someProperty: RestrictedType

This denotes the type of all values such that all objects of

`someProperty`

are instances of `RestrictedType`

.Similarly, the existential range restriction is built using the

`any`

keyword:any someProperty: RestrictionType

This denotes the type of all values such that at least one object of

`someProperty`

is an instance of `RestrictedType`

.Range restrictions are usually part of an intersection:

Type & all someProperty: RestrictionType

Last modified 1mo ago

Copy link

On this page

Definition

Type Expression