Boolean
The boolean data type in Filtrera represents truth values. This reference guide provides a detailed specification of the boolean data type, its syntax, and usage for users already familiar with the concept.
Syntax
The type notation for boolean can be either boolean or bool. The boolean type is actually a union of the types true and false.
Basic Syntax
let isActive: boolean = trueIn this example, isActive is defined as a boolean type and assigned the value true.
Type Notation for boolean
The boolean type can be specified using boolean or bool.
Example
param isAvailable: boollet status = isAvailable match true |> 'Available' false |> 'Not Available'from statusIn this example, isAvailable is defined as a bool type, and status is determined based on the value of isAvailable using pattern matching.
Boolean as Literal Types
Although boolean behaves similarly to a literal type, true and false are distinct types themselves and cannot hold values.
Example
let isTrue: true = truelet isFalse: false = false
let messageTrue = $'The value is {isTrue}.'let messageFalse = $'The value is {isFalse}.'
from messageTruefrom messageFalseIn this example, isTrue is of type true and isFalse is of type false. The messages incorporate these values.
Logical Operators
Filtrera supports three logical operators for boolean expressions: and, or, and not.
Examples
and Operator
let a = truelet b = falselet conjunction = a and b
from conjunctionIn this example, conjunction is false because a and b evaluates to false.
or Operator
let a = truelet b = falselet disjunction = a or b
from disjunctionIn this example, disjunction is true because a or b evaluates to true.
not Operator
let a = truelet negation = not a
from negationIn this example, negation is false because not a evaluates to false.
Practical Usage
Example: Conditional Logic
param isLoggedIn: booleanlet greeting = isLoggedIn match true |> 'Welcome back!' false |> 'Please log in.'
from greetingIn this example, isLoggedIn determines the value of greeting based on whether the user is logged in using pattern matching.
Example: Complex Boolean Expressions
let a = truelet b = falselet c = true
let complexExpression = (a and b) or (not c)
from complexExpressionIn this example, complexExpression is evaluated using a combination of and, or, and not operators.
Summary
The boolean data type in Filtrera allows you to work with truth values effectively. By understanding its syntax and usage, including the type notations boolean and bool, you can create clear and precise logical expressions. The boolean type, being a union of true and false, provides flexibility in parameter and variable definitions while maintaining the distinct types true and false. Filtrera’s logical operators and, or, and not, along with pattern matching, enable you to construct complex boolean expressions. Leveraging these features enables you to create robust Filtrera programs that handle boolean logic with ease.