Skip to content
Draft

Conditions

Conditions control the flow of execution based on boolean expressions.

The basic if block starts with if and ends with fin if.:

if @ready
launch!
fin if.
if @score >= 90
write("A")
else
write("Not A")
fin if.
if @score >= 90
write("A")
else if @score >= 80
write("B")
else if @score >= 70
write("C")
else if @score >= 60
write("D")
else
write("F")
fin if.

Conditions must evaluate to Boolean:

@// Valid
if @active
if @count > 0
if @name == "test"
@// Invalid (will not compile)
if @count @// Error: expected Boolean, got Int

Combine conditions with and, or, not:

if @age >= 18 and @has_id
write("Entry allowed")
fin if.
if @status == "active" or @status == "pending"
write("Processing")
fin if.
if not @disabled
write("Enabled")
fin if.
if @authenticated
if @admin
write("Admin panel")
else
write("User panel")
fin if.
else
write("Please log in")
fin if.

For complex branching, use match:

match @status
when "active" then write("Running")
when "paused" then write("Paused")
when "stopped" then write("Stopped")
else write("Unknown status")
fin match.
match @value
when String then write("String: " + @value)
when Int then write("Int: " + string(@value))
when Boolean then write("Boolean: " + string(@value))
else write("Unknown type")
fin match.
match @score
when @s if @s >= 90 then write("A")
when @s if @s >= 80 then write("B")
when @s if @s >= 70 then write("C")
else write("F")
fin match.

Use if as an expression:

@if @score >= 60
@result = "pass"
else
@result = "fail"
fin if.
@// Or inline
@result = if @score >= 60 then "pass" else "fail" fin if.

Inverse of if (executes when condition is false):

unless @authenticated
redirect("/login")
fin unless.

and and or short-circuit:

@// If @list is nil, @list#length is never called
if @list != nil and @list#length > 0
process(@list)
fin if.
action process(@data: String?)
if @data == nil
return
fin if.
@// Process data...
fin action.
action divide(@a: Float, @b: Float) -> Float?
if @b == 0.0
return none
fin if.
return some(@a / @b)
fin action.
@label = if @active then "Active" else "Inactive" fin if.

Continue to Loops for iteration constructs.