Skip to content

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 = true

In 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: bool
let status = isAvailable match
true |> 'Available'
false |> 'Not Available'
from status

In 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 = true
let isFalse: false = false
let messageTrue = $'The value is {isTrue}.'
let messageFalse = $'The value is {isFalse}.'
from messageTrue
from messageFalse

In 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 = true
let b = false
let conjunction = a and b
from conjunction

In this example, conjunction is false because a and b evaluates to false.

or Operator

let a = true
let b = false
let disjunction = a or b
from disjunction

In this example, disjunction is true because a or b evaluates to true.

not Operator

let a = true
let negation = not a
from negation

In this example, negation is false because not a evaluates to false.

Practical Usage

Example: Conditional Logic

param isLoggedIn: boolean
let greeting = isLoggedIn match
true |> 'Welcome back!'
false |> 'Please log in.'
from greeting

In 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 = true
let b = false
let c = true
let complexExpression = (a and b) or (not c)
from complexExpression

In 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.