Negated Types
Negated types in Filtrera provide a way to define types that exclude specific values. This reference guide provides a detailed specification of negated types, their syntax, and usage for users already familiar with the concept.
Negated types allow you to specify that a type should not include certain values. This is useful for creating more precise type definitions and for handling exceptions. Negated types are often used in combination with intersection types to allow certain types while excluding specific values.
Syntax
Negated types are created using the not
operator. The not
operator precedes the type or value that should be excluded.
Example
not <type>
Practical Usage
Example: Excluding a Specific Value
Negated types can be used to exclude specific values from a type. For example, you can define a type that includes all text
values except an empty string.
let NonEmptyText: text & not ''
In this example, NonEmptyText
is a type that includes any text
value except the empty string.
Example: Using Negated Types in Functions
You can use negated types in function definitions to ensure that certain values are excluded from the input.
let greet = (name: text & not '') => 'Hello, {name}!'
let result1 = greet('Alice') // Validlet result2 = greet('') // Invalid
from result1from result2
In this example, the greet
function accepts any text
value except an empty string. If an empty string is passed, it will result in an error.
Example: Combining Negated Types with Other Types
Negated types can be combined with other types using intersection types to create more complex type definitions.
let validIdentifier: text & not ('true' | 'false' | '')
In this example, validIdentifier
is a type that includes any text
value except the strings 'true'
, 'false'
, and an empty string.
Example: Pattern Matching with Negated Types
Negated types can be used in pattern matching to exclude specific values from being matched.
let describeValue = (v: text) => from v match text & not '' |> 'Non-empty text: {v}' '' |> 'Empty text'
let result1 = describeValue('Hello')let result2 = describeValue('')
from result1from result2
In this example, describeValue
matches non-empty text and empty text separately using negated types.
Summary
Negated types in Filtrera provide a powerful mechanism for excluding specific values from a type. By using the not
operator, you can create precise type definitions and handle exceptions effectively. Negated types are often used in combination with intersection types to allow certain values while excluding others, enhancing the flexibility and robustness of your Filtrera programs.
This technical specification article provides a comprehensive reference for negated types in Filtrera, including details about their syntax, practical usage, and examples.