Control Flow
Dryft provides minimal but powerful control flow constructs:when for conditionals, then for simple branches, and cycle for loops. This simplicity eliminates the need for multiple overlapping control structures.
Why then, when, and cycle?
Dryft’s philosophy is to keep syntax as simple as possible. Traditional languages have many control structures that are just syntactic sugar. Dryft consolidates them:then- Simple conditional statementswhen- Multi-way branching (if/else, switch, ternary)cycle- All loop types (while, do-while, for, infinite)
Conditionals with then
Thethen keyword creates a simple conditional that executes when the top of stack is true.
Syntax
Examples
Type Requirement
then expects a Binary (boolean) value on the stack:
Multi-way Branching with when
Thewhen keyword handles if/else, switch statements, and ternary operations.
Syntax
when block evaluates branches in order and executes the first matching condition. The final clause (without then) is the default case.
If/Else Pattern
Switch/Match Pattern
Without Default Case
Loops with cycle
Thecycle keyword creates loops. All loop types (while, do-while, for, infinite) are implemented using cycle with break.
Syntax
Breaking Loops
Use thebreak keyword to exit a cycle:
Pre-check Loop (while)
Post-check Loop (do-while)
Infinite Loop
Counting Loop (for)
Variables
Variables store values within a scope. They work with Dryft’s linear type system.Declaring Variables
Reading Variables
Prefix with$ to read a variable’s value:
Writing Variables
Suffix with! to write a new value:
Variable Scope
Variables are scoped to their defining block:Variables in Loops
The return Keyword
Usereturn to exit early from a function or action:
Complete Examples
FizzBuzz
Factorial
Exponentiation
Safe Division
Type Footprint
All blocks must have a balanced type footprint:Summary
then- Execute code when condition is truewhen- Multi-way branching for if/else and switchcycle- Universal loop construct withbreakvar:- Declare variables ($read, write!)return- Early exit from functions/actions