library¶
library
is a top level declaration containing functions implementing common business rules and utilities.
Unlike an ALFA service which defines a ‘contract’ or interface without an implementation, a library
contains
implementations of functions.
The use of library is optional, however common rules and logic can be encapsulated in a library
to be reused from assert
or other ALFA expression statements. Using ALFA’s powerful expression syntax, library
functions can implement data manipulation
and validation logic.
Similar to ALFA top level constructions such as record, logic implemented in a library
can be code-generated into
native code.
A library
can be declared within a namespace, and has a given name.
Example Library
library CountryGroups {
isG7( countryCode : string ) : boolean {
let g7 = { "CA", "FR", "DE", "IT", "JP", "UK", "US" }
return contains( g7, countryCode )
}
isOpec( countryCode : string ) : boolean {
let opec = { "IRN", "IRQ", "DZA", "SAU", "IDN", "VEN", "LBY", "ARE", "NGA", "KWT", "ECU", "AGO", "GAB" }
return contains( opec, countryCode )
}
}
Syntax of a library function
<function name>( <list of parameters> ) : <result type> {
<statements>
}
Functions can refer to existing ALFA definitions for data types.