typedefs

A typedefs declaration can be used to create new types based on existing data types including existing typedefs. It can also be used to define ‘external’ types to allowing custom types to be implemented outside of ALFA.

typedefs is a top level declaration with global scope, and its entries are available for use in any user-defined type. Generic types can be defined combining existing types.

typedefs {
    // An unsigned int type
    uint = int(0,*)
    // A dictionary type with a string key and generic value
    dictionary< T > = map< string, T >
}

record Team {
    Name : string
    Points : uint
    // Using dictionary typedef with int value
    PlayerAndRank : dictionary< int >
}

The declaration order of typedefs has no effect. A typedef can be referenced before it is declared.

typedefs {
  money = ccy
  ccy = string
}

Use of typedefs is optional, however, its use can encourage creation of more consistent and clear data models.

External typedefs

Although there are numerous benefits of strict datatypes, there are cases where flexibility is required.

ALFA already supports an extensive set of datatypes and validation rules, but it can be further extended with ‘external’ data types.

These are for cases where real-world usecases demand other conditions or considerations that cannot simply be modelled with a fixed set of data types and values.

Consider example models:

  • Tenor - in Finance where periods are expressed in days, weeks, months, years interchangeably with some values being equivalent (e.g. 12M == 1Y). New values may get added occasionally.
  • Slow-changing set of predefined values - based on user needs, new values get added periodically to the set of valid values.

Although values can be validated with ALFA rules, there is a better alternative. ALFA allows such types to be externalized by using typedefs external declarations.

An external type is declared as follows:

typedefs {
  tenor = external acme.financial.Tenor
}
  • tenor is the new type being introduced.
  • acme.financial.common.Tenor is the external runtime implementation that will validate values, check equivalence, serialize to and from string.

With that in place, the new type can be used as a normal data type.

record Loan {
    Period : tenor
}

An ALFA external type is implemented as code (e.g. a Java/C++/C#/Python class) in a target language that implements the required logic. The external type implementation needs to be capable of serializing and deserializing values as string in order to comply with ALFA runtime serialization support.