Text
The text
data type in Filtrera is used to represent sequences of characters, or strings. This reference guide provides a detailed specification of the text
data type for users already familiar with the concept.
Literal Syntax
Literal text values can be wrapped in either single ('
) or double ("
) quotes. The chosen quote character must be used to terminate the literal.
Basic Syntax
let single_quoted = 'Hello, World!'let double_quoted = "This works just as well."
To include the quote character within the literal, it must be escaped by doubling it.
let single_with_escape = 'Hello, ''World!''' // Evaluates to "Hello, 'World!'"let double_with_escape = "She said, ""Hello!""" // Evaluates to 'She said, "Hello!"'
Template Strings
Template strings allow for the embedding of expressions directly within a text literal. A template string is created by prefixing a text literal with one or more dollar signs ($
).
The number of dollar signs determines the number of curly braces required to denote an interpolated expression. This provides a powerful and unambiguous way to include literal curly braces in your output.
Basic Interpolation
A single $
prefix means that expressions wrapped in single curly braces ({...}
) will be evaluated and inserted into the text.
let name = 'Alice'let greeting = $'Hello, {name}!' // Evaluates to "Hello, Alice!"
Including Literal Braces
To include literal curly braces that are not processed as part of an interpolation, you increase the number of dollar signs. The $
prefix sets the “interpolation level”; any sequence of curly braces less than that level will be treated as literal text.
If you prefix a string with $$
, only expressions inside double curly braces ({{...}}
) are interpolated. Any single curly braces ({...}
) are treated as literal characters.
Example: Including literal {}
let placeholder = 'name'
// This would incorrectly interpolate the `placeholder` variable:let example1 = $'Use {placeholder} for interpolation.' // "Use name for interpolation."
// By using $$, we tell the parser to only look for {{...}} to interpolate.// Since it finds none, {placeholder} is treated as literal text.let example2 = $$'Use {placeholder} for interpolation.' // "Use {placeholder} for interpolation."
This mechanism allows you to mix literal braces and interpolated expressions in the same string with perfect clarity.
Example: Mixing literal and interpolated braces
let user = 'Alice'
// Here, $$ sets the interpolation level to two.// {{user}} matches the level and is interpolated.// {user} does not match and is treated literally.let message = $$'The syntax is {user}, but the interpolated value is {{user}}.'// Evaluates to "The syntax is {user}, but the interpolated value is Alice."
Constraints
Like other primitive types in Filtrera, text
can be further constrained to define more specific types.
Literal Constraint
A literal constraint narrows the type to a single, specific text value. When combined with a Union Type, this allows you to define a type that can only be one of several specific text values. This is extremely useful for parameters and state machines.
Example
param status: 'active' | 'inactive' | 'pending'
let message = $'The current status is {status}.'from message
In this example, the status
parameter is guaranteed to be one of the three specified literal text values.
Regular Expression Pattern
A regular expression pattern can be used to constrain a text value to a specific format. This is especially useful for input validation and in match
expressions.
Example
from input match /hot dog/ |> 'It''s a 🌭' /hot/ |> 'It''s something hot' /dog/ |> 'It''s something dog-ish'
Summary
The text
data type in Filtrera is a versatile tool for handling string values. It supports both single and double-quoted literals for flexibility. Its powerful template string system, which uses a variable number of $
prefixes, allows for clear and unambiguous embedding of expressions while making it trivial to include literal curly braces in the output. Furthermore, by using literal and regular expression constraints, you can create highly specific and robust text-based types in your Filtrera programs.