Nothing
The nothing
data type in Filtrera is a special type that represents the absence of a value. This reference guide provides a detailed specification of the nothing
data type, its syntax, and usage for users already familiar with the concept.
The nothing
type is used to represent a value that does not exist or is not applicable. It is not compatible with any other type and is typically used in situations where a condition does not match or when no pattern matches the input in pattern matching.
Usage
The nothing
type is often used in filters and pattern matching to indicate the absence of a value.
Parameters
nothing
can be used to indicate an optional parameter without a default value
param input: text | nothing
from input match text |> 'input was a text'
In this example, the returned value is ‘input was a text’ if the input parameter was set. Otherwise nothing is returned.
Filters
In filters, nothing
is returned when a condition does not match.
let isEven = (x: number) => x mod 2 == 0let filterEvenNumbers = (numbers: [number]) => numbers where (x => isEven(x))
let result = filterEvenNumbers([1, 2, 3, 4, 5])
from result
In this example, the filterEvenNumbers
function filters out even numbers from the list. For odd numbers, the filter condition does not match, and those values effectively become nothing
.
Pattern Matching
In pattern matching, nothing
is used when no case matches the input.
let describeValue = (v: text | number) => from v match x: text |> $'Text: {x}' x: number |> $'Number: {x}'
from describeValue('Hello')from describeValue(42)from describeValue(true) // This will result in nothing
In this example, describeValue
returns a string description for text
and number
types. If a different type is passed, it returns nothing
.
Summary
The nothing
data type in Filtrera represents the absence of a value and is not compatible with any other type. It is commonly used in filters and pattern matching to indicate when a condition does not match or when no case matches the input. By understanding its usage and handling it appropriately, you can create more robust and error-tolerant Filtrera programs.