Booleans
Booleans represent truth values in Harneet. They are fundamental for control flow, conditional logic, and boolean algebra.
Overview
Booleans in Harneet: - Two values - true and false - No implicit conversion - must be explicitly boolean - Logical operators - and, or, not with short-circuit evaluation - Method-rich - built-in .toInt() and .toString() methods - Comparison results - all comparison operators return booleans
Boolean Literals
| Boolean Literals |
|---|
| package main
var isActive = true
var isDisabled = false
|
Boolean Instance Methods
toInt()
Converts the boolean to an integer (0 for false, 1 for true):
| toInt() |
|---|
| package main
var t = true
var f = false
var tInt = t.toInt() // 1
var fInt = f.toInt() // 0
// Useful for counting or scoring
var questions = [true, false, true, true]
var correct = 0
for answer in questions {
correct = correct + answer.toInt()
}
// correct = 3
|
toString()
Converts the boolean to a string representation:
| toString() |
|---|
| package main
var t = true
var f = false
var tStr = t.toString() // "true"
var fStr = f.toString() // "false"
// Useful for logging or display
var isLoggedIn = true
var message = "User logged in: " + isLoggedIn.toString()
// "User logged in: true"
|
Logical Operators
Harneet uses Python-style logical operators with short-circuit evaluation:
and
Returns true only if both operands are true:
| and Operator |
|---|
| package main
var result1 = true and true // true
var result2 = true and false // false
var result3 = false and true // false
var result4 = false and false // false
// Short-circuit: right side not evaluated if left is false
var x = false and expensiveFunction() // expensiveFunction() not called
|
or
Returns true if at least one operand is true:
| or Operator |
|---|
| package main
var result1 = true or true // true
var result2 = true or false // true
var result3 = false or true // true
var result4 = false or false // false
// Short-circuit: right side not evaluated if left is true
var x = true or expensiveFunction() // expensiveFunction() not called
|
not
Negates a boolean value:
| not Operator |
|---|
| package main
var result1 = not true // false
var result2 = not false // true
var isDisabled = not isEnabled
|
Comparison Operators
All comparison operators return boolean values:
| Comparison Operators |
|---|
| package main
var a = 10
var b = 20
var equal = a == b // false
var notEqual = a != b // true
var less = a < b // true
var lessEqual = a <= b // true
var greater = a > b // false
var greaterEqual = a >= b // false
|
Boolean Expressions
Combine booleans and comparisons to create complex conditions:
| Boolean Expressions |
|---|
| package main
var age = 25
var hasLicense = true
var hasInsurance = true
var canDrive = age >= 18 and hasLicense and hasInsurance
var isWeekend = day == "Saturday" or day == "Sunday"
var isHoliday = not isWorkday
|
None-Receiver Safety
Boolean instance methods enforce None-receiver safety:
| None-Receiver Safety |
|---|
| package main
var b bool = None
// These will raise runtime errors:
// b.toInt() // ERROR: type null does not have method 'toInt'
// b.toString() // ERROR: type null does not have method 'toString'
|
Control Flow
Booleans are essential for control flow statements:
if Statements
| if Statements |
|---|
| package main
import fmt
var isValid = true
if isValid {
fmt.Println("Valid input")
} else {
fmt.Println("Invalid input")
}
|
while Loops
| while Loops |
|---|
| package main
var running = true
var count = 0
while running {
count = count + 1
if count >= 10 {
running = false
}
}
|
for Loops with Conditions
| for Loops |
|---|
| package main
var items = [1, 2, 3, 4, 5]
var found = false
for item in items {
if item == 3 {
found = true
break
}
}
|
Complete Example
| Complete Example |
|---|
| package main
import fmt
// Boolean methods
var isActive = true
var isDisabled = false
fmt.Println("Active as int:", isActive.toInt()) // 1
fmt.Println("Disabled as int:", isDisabled.toInt()) // 0
fmt.Println("Active as string:", isActive.toString()) // "true"
fmt.Println("Disabled as string:", isDisabled.toString()) // "false"
// Logical operators
var hasPermission = true
var isAuthenticated = true
var canAccess = hasPermission and isAuthenticated
fmt.Println("Can access:", canAccess) // true
// Counting with toInt()
var flags = [true, false, true, true, false]
var trueCount = 0
for flag in flags {
trueCount = trueCount + flag.toInt()
}
fmt.Println("Number of true flags:", trueCount) // 3
// Building messages
var status = "System is " + isActive.toString()
fmt.Println(status) // "System is true"
|
Use Cases
Counting Boolean Values
| Counting Booleans |
|---|
| package main
var answers = [true, true, false, true, false]
var correctCount = 0
for answer in answers {
correctCount = correctCount + answer.toInt()
}
// correctCount = 3
|
Feature Flags
| Feature Flags |
|---|
| package main
var enableDarkMode = true
var enableNotifications = false
var config = {
"darkMode": enableDarkMode.toString(),
"notifications": enableNotifications.toString()
}
// {"darkMode": "true", "notifications": "false"}
|
Conditional Scoring
| Conditional Scoring |
|---|
| package main
var achievements = [true, true, false, true]
var score = 0
for achievement in achievements {
score = score + (achievement.toInt() * 10)
}
// score = 30
|
Best Practices
1. Use Descriptive Names
| Descriptive Names |
|---|
| // Good: Clear intent
var isAuthenticated = true
var hasPermission = false
var canEditDocument = isAuthenticated and hasPermission
// Avoid: Unclear
var flag1 = true
var flag2 = false
|
2. Prefer Explicit Comparisons for Clarity
| Explicit Comparisons |
|---|
| // Preferred: Clear comparison
if userCount > 0 {
// ...
}
// Also valid but less explicit
if userCount {
// This won't work in Harneet - no implicit conversion
}
|
3. Use Short-Circuit Evaluation
| Short-Circuit Evaluation |
|---|
| // Efficient: stops after first false
if isValid and checkDatabase() and processData() {
// checkDatabase() only called if isValid is true
}
|
See Also