Structure
MTLQuery
Represents an MTL query - a side-effect-free operation returning a value.
struct MTLQuery
Overview
Queries provide reusable computation logic in MTL modules, extending AQL with custom operations that can be invoked from templates and other queries. Unlike templates, queries do not generate text output.
Overview
MTL queries serve multiple purposes:
-
Code reuse: Complex expressions can be encapsulated and reused
-
Type extension: New operations can be added to existing types
-
Modularity: Complex templates can be broken down into manageable functions
-
Testing: Individual query functions can be tested independently
Side-Effect Freedom
Queries must be side-effect-free, meaning they:
-
Do not modify model elements
-
Do not write to files or output
-
Only compute and return values
-
Can be safely cached and memoized
Example Usage
// Simple query
let fullNameQuery = MTLQuery(
name: "fullName",
parameters: [
MTLVariable(name: "person", type: "Person")
],
returnType: "String",
body: MTLExpression(AQLBinaryExpression(
left: AQLNavigationExpression(
source: AQLVariableExpression(name: "person"),
property: "firstName"
),
right: AQLNavigationExpression(
source: AQLVariableExpression(name: "person"),
property: "lastName"
),
op: .concat
))
)
// Type-checking query
let isPublicQuery = MTLQuery(
name: "isPublic",
parameters: [MTLVariable(name: "element", type: "Element")],
returnType: "Boolean",
body: MTLExpression(AQLNavigationExpression(
source: AQLVariableExpression(name: "element"),
property: "visibility"
))
)
Topics
Initializers
Creates a new MTL query.
Instance Properties
The query body expression.
Optional documentation string.
The name of the query.
The parameters accepted by this query.
The return type of the query.
The visibility of this query.
Default Implementations
Relationships
Conforms To
See Also
Module Structure
Represents an MTL (Model-to-Text Language) module.
Represents an MTL template for text generation.