AML Constant
Introduction
An AML constant allows you to specify a value that can be reused throughout a project.
- Once a constant is declared, user can not change its value through reassignment (it is immutable)
- Cannot declare same name constant in the same scope (i.e. AML modules and functions)
Syntax
There are two equivalent ways to declare a new constant. You can declare the constant using the const
keyword without explicit type:
const <constant-name> = <expression>
or you can explicitly declare the constant’s type
<constant-type> <constant-name> = <expression>
In the first case, the type of const variable is automatically inferred from its value.
Example usages
Basic types
const a = 'hello' // a's type is String
const b = 2 // b's type is Number
// Explicit type declaration
String c = 'world'
Number d = 2.5 + b // 4.5
Int e = 3
// Use the constant with string interpolation
const signup_threshold = 0.6 // signup_threshold's type is Number
Dataset my_dataset {
metric count {
definition: @aql count(*)
| where users.signup_threshold < ${signup_threshold}
;;
}
}
tip
String interpolation is a common way to reuse basic constant types.
Lists and dictionaries
- Declare with
const
keyword
// AML list
const models = ['users', 'countries', 'orders']
// access to list's elements
models(0) // 'users'
models(1) // 'countries'
models(2) // 'orders'
// AML dictionary
const modelDict = {
users: 'users'
countries: 'countries'
orders: 'orders'
}
// access to dictionary's elements
modelDict('users') // 'users'
modelDict('countries') // 'countries'
modelDict('orders') // 'countries'
- Explicit type
// AML list
List<String> models = ['users', 'countries', 'orders']
// access to list's elements
models(0) // 'users'
models(1) // 'countries'
models(2) // 'orders'
// AML dictionary
Type StringDict = Dict<String, String>
StringDict modelDict = {
users: 'users'
countries: 'countries'
orders: 'orders'
}
Object types
const my_filter = FilterBlock { // my_filter's type is FilterBlock
type: 'field'
label: 'User role'
source: FieldFilterSource {
dataset: 'tenant_user'
field: ref('public_users', 'role')
}
}
const my_dashboard_1 = Dashboard {
...
block f1: my_filter // reuse my_filter in multiple dashboards
...
}
Dashboard my_dashboard_2 {
...
block f2: my_filter // reuse my_filter in multiple dashboards
...
}
Model extended_users = users.extend({
label: 'Extended users'
measure count {
label: 'count'
type: 'number'
definition: @aql sum(extended_users.id) ;;
}
}
// equivalent to
const extended_users2 = users.extend({
label: 'Extended users'
measure count {
label: 'count'
type: 'number'
definition: @aql sum(extended_users.id) ;;
}
}
Use-cases
- Define a set of variables and reuse them in multiple places
- Define & reuse global SQL definitions
- Build a Dashboard with Multiple Similar Charts