Skip to main content

AML If-else

Introduction

AML if-else is a control-flow expression. It evaluates a Boolean condition and returns the value of the executed block. The value of the if-else expression is the value of the last expression inside the executed block.

  • When used as a standalone statement, else is optional.
  • When used within another expression (for example, assignment or string interpolation), the expression must evaluate to a value on all paths; include an else (or else if … else) branch.

Syntax

// single branch
if (condition) {
expression
}

// with else clauses
if (condition1) {
expression1
} else if (condition2) {
expression2
} else {
expression3
}
  • Parentheses are required around conditions.
  • Braces are required around blocks.
  • Conditions must be Boolean expressions.

Examples

const a = 1

if (a > 0) {
'positive'
} else if (a == 0) {
'zero'
} else {
'negative'
}
// returns 'positive'
const a = 1

const b = if (a > 0) {
'positive'
} else if (a == 0) {
'zero'
} else {
'negative'
}

b // 'positive'

String interpolation example:

Func greet(name: 'John' | 'Alice') {
"Hello, ${if (name == 'John') { 'sir' } else { 'madam' }}!"
}

Analytics-focused examples

Conditional metric selection in a KPI block:

// Renders a KPI block for either revenue (sum) or orders (count)
Func kpi_block(metric: 'revenue' | 'orders') {
const measure_ref = if (metric == 'revenue') {
r(fact_orders.revenue)
} else {
r(fact_orders.id)
}

VizBlock {
label: "KPI - ${metric}" // dynamic label derived from selected metric
viz: SingleValue {
dataset: 'ecommerce'
series {
field {
ref: measure_ref
aggregation: if (metric == 'revenue') { 'sum' } else { 'count' }
}
}
}
}
}

Conditional grouping dimension for a distribution chart:

// Switches the grouping between product category and country
Func distribution_block(by: 'category' | 'country') {
const group_dim = if (by == 'category') {
r(products.category)
} else {
r(countries.name)
}

VizBlock {
label: "Distribution by ${by}" // dynamic label derived from grouping parameter
viz: PieChart {
dataset: 'ecommerce'
legend: group_dim
series {
field {
ref: r(orders.id)
aggregation: 'count'
}
}
}
}
}

Type of an if-else expression

The type is the union of all possible branch result types.

Func gen_dashboard_block(block_type: DashboardBlock) {
if (block_type == VizBlock) {
VizBlock { ... }
} else if (block_type == FilterBlock) {
FilterBlock { ... }
} else {
TextBlock { ... }
}
}

// gen_dashboard_block return type: VizBlock | FilterBlock | TextBlock

Guidelines:

  • Branches used in an expression context should return compatible types or a meaningful union.
  • If a branch yields no value, it cannot be used where a value is required.

AML comparison operators

  • ==: true if operands are equal
  • !=: true if operands are not equal
  • >: true if left is greater than right
  • >=: true if left is greater than or equal to right
  • <: true if left is less than right
  • <=: true if left is less than or equal to right

Examples:

3 == 3   // true
4 != 5 // true
5 > 2 // true
2 >= 2 // true
1 < 3 // true
2 <= 1 // false

AML logical operators

  • &&: true if both operands are true
  • ||: true if at least one operand is true
  • !: logical negation

Examples:

true && false  // false
true && true // true

true || false // true
false || false // false

!true // false
!false // true

Let us know what you think about this document :)