1. Boolean Operations

10. Boolean Operations

Use pattern matching for conditional logic:

Examples:

let result = match x > 0 {
    true => "positive"
    false => "zero or negative"
}

let max = match a > b {
    true => a
    false => b
}

10.1 Boolean Pattern Matching #

Osprey uses pattern matching instead of traditional if-else statements for boolean operations. This ensures exhaustive handling of both true and false cases.

Basic Boolean Matching:

let status = match isValid {
    true => "Success"
    false => "Failure"
}

Complex Boolean Logic:

let category = match score >= 90 {
    true => match score == 100 {
        true => "Perfect"
        false => "Excellent"
    }
    false => match score >= 70 {
        true => "Good"
        false => "Needs Improvement"
    }
}

10.2 Boolean Operators #

  • && - Logical AND
  • || - Logical OR
  • ! - Logical NOT
  • == - Equality
  • != - Inequality
  • >, <, >=, <= - Comparison operators

Operator Examples:

let isAdult = age >= 18
let hasPermission = isAdult && isAuthorized
let canAccess = hasPermission || isAdmin
let isBlocked = !isActive