Built-in Functions

The functions a constraint can call, with the types they take and return.

Function Result

$abs(x: number): number

The absolute value of x.

$average(x: list<number>): float?

The mean of the numbers in x, no value for an empty list.

$ceil(x: number): int

The smallest integer not below x, in the unit of x.

$cfgByDefinition(name: 'Def'): entity?

The root of another definition’s configuration in the CfgSpace, no value when there is none.

$contains(text: string, search: string): bool

Whether the string text contains the string search.

$count(x: list<T>): int

The number of elements of x.

$def(x: T): def

The definition element declaring x, to read the annotations the definition writes on it.

$distinct(x: list<T>): list<T>

The elements of x with every duplicate removed.

$every(x: list<T>, cond: ($: T) => bool): bool

Whether every element of x satisfies cond. A failing constraint names the first failing element.

$exists(x: T): bool

Whether x has a value, a parameter with a default always has one.

$filter(x: list<T>, cond: ($: T) => bool): list<T>

The elements of x satisfying cond, like x[cond], but a named lambda parameter such as $t keeps the outer context for bare names.

$floor(x: number): int

The largest integer not above x, in the unit of x.

$isType(x: entity, type: 'Def::Type'): bool

Whether x is of type or a subtype, and narrows x to it.

$length(text: string): int

The number of Unicode code points of text, as maxLength counts them.

$lowercase(text: string): string

text in lower case.

$max(x: list<number>): number?

The largest of the numbers in x, no value for an empty list.

$meta(x: T): meta

The $ meta fields of x, .description reading its $description.

$min(x: list<number>): number?

The smallest of the numbers in x, no value for an empty list.

$noOverlap(x: list<T>, start: ($: T) => number, end: ($: T) => number): bool

Whether no two half-open intervals [start, end) of x overlap. A violation names both overlapping elements.

$not(b: bool): bool

The negation of the boolean b.

$pad(text: string, width: int, char?: string): string

text padded with char, a space by default, to width characters: on the right for a positive width, on the left for a negative one.

$path(x: entity): string

The reference path of the entity x, /Mcu/Board/Can0, which names it unambiguously in a message.

$reference(refParam: ref): ref

The reference instead of its target.

$referencedBy(x: entity, refParam: 'Def::Type::RefParam'): list<entity>

The parents of every refParam naming the entity x, named by its type path or as 'Def::Type.refParam'.

$replace(text: string, pattern: string, replacement: string, limit?: int): string

text with each occurrence of pattern replaced, at most limit of them.

$round(x: number): int

x rounded to the nearest integer in its unit, a half away from zero.

$single(x: list<T>, cond?: ($: T) => bool): T

The one element of x satisfying cond, or the one element without cond. No or several matches fail the constraint.

$some(x: list<T>, cond: ($: T) => bool): bool

Whether at least one element of x satisfies cond.

$split(text: string, separator: string, limit?: int): list<string>

The parts of text between the occurrences of separator, the characters for an empty one, at most limit parts.

$substring(text: string, start: int, length?: int): string

The characters of text from start, a negative one counting from the end, at most length of them.

$substringAfter(text: string, search: string): string

The part of text after the first search, text whole when search does not occur.

$substringBefore(text: string, search: string): string

The part of text before the first search, text whole when search does not occur.

$sum(x: list<number>): number

The sum of the numbers in x, 0 for an empty list.

$trim(text: string): string

text without leading and trailing whitespace.

$unique(x: list<T>): bool

Whether no two elements of x are equal. A violation names the duplicates.

$uppercase(text: string): string

text in upper case.

Each example holds for the sample configuration, on the element in the first column:

Constraint on Expression Validates

board

$single(canControllers, $ => mode == 'FD').nominalBitrate <= '1 MHz'

A value of the one FD controller, failing on none or several.

ports

!$isType($instance, 'Mcu::UartPort') || baudrate <= 115200

A property of one member type of a union.

board

( $p := $cfgByDefinition('Project'); !$exists($p) || $count(tasks) <= $p.limit.maxTaskCount )

A limit from the configuration of the sample’s second definition, Project.

priority

$instance <= ($def($instance)._limit.max ?? 255)

A bound from the _limit annotation the definition writes on the parameter.

$meta($reference(coreRef)) describes the reference, $meta(coreRef) the core.

Lambdas describes the lambda arguments, Annotations how a constraint reads an annotation.