Basic building blocks

Records

The simplest definition is a record. You create a named definition with some associated fields. The fields are declared within parenthesis in the form <field name> : <data type>.

record Employee {
   Id : uuid
   StartDate : date
   Name : string
}

Enumerations

To define a set of constant values, enum can be used. These are typically used to capture values which rarely change and where it is better than representing a value simply as a string.

enum Direction {
   North South East West
}

Entity - Uniquely identifiable objects

An entity distinguishes itself from a record by having an associated key.

Being able to uniquely identify an object is an important aspect in modelling. This is analogous to a primary key in relational databases. By defining an entity instead of record, you can express object identity.

entity Flight key( Id : uuid ) {
   Code : string
   Departure : time
}

Reusability

Keys

When creating an entity, instead of defining the key each time, a reusable key can be defined and used for the entity.

key AccountId {
   AccountNo : string
   SortCode : string
}

entity CurrentAccount key AccountId {
   ...
}

Sharing Common Characteristics

Often similar objects share a common set of fields in addition some others which are unique to them - it can be said they share common characteristics or ‘traits’.

ALFA support defining trait to capture common characteristics of objects. Those traits can be included into other definitions. Traits can also include other traits.

When a trait is included, all its fields, asserts, and annotations are inherited into the target definition.

trait Shape {
   Colour : string
}

record Rectangle includes Shape {
   Width : int
   Height : int
}

record Circle includes Shape {
   Radius : int
}