WHEN organizes code execution into distinct block types, each with specific behavior and use cases. This replaces traditional loop constructs with declarative execution models.
The main:
block is the program entry point and runs continuously.
main:
# Executes every frame/iteration
when some_condition:
start_task()
when task_complete:
print("Task finished!")
exit()
Forever blocks run continuously until explicitly stopped.
fo background_task():
perform_background_work()
sleep(1) # Prevent tight loops
fo game_loop():
update_game_state()
render_frame()
sleep(0.016) # ~60 FPS
when game_over:
break # Exit the block
break
or .stop()
parallel fo
for separate threadDeclarative blocks execute a fixed number of iterations.
de process_items(10):
current_item = items[process_items.current_iteration]
process(current_item)
print(f"Processed {process_items.current_iteration + 1}/10")
# Variable iteration count
item_count = len(my_list)
de dynamic_processor(item_count):
item = my_list[dynamic_processor.current_iteration]
handle_item(item)
.current_iteration
de worker(5):
print(f"Iteration: {worker.current_iteration}")
print(f"Status: {worker.status}") # RUNNING, STOPPED, COMPLETED
print(f"Total iterations: {worker.iterations}")
One-shot blocks execute immediately when called, useful for recursive operations.
# Recursive list processing
current_index = 0
items = ["a", "b", "c", "d"]
os process_next():
when current_index < len(items):
print(f"Processing: {items[current_index]}")
current_index = current_index + 1
process_next() # Recursive call
main:
process_next() # Start processing
Add parallel
keyword to run blocks in separate threads.
# Parallel forever block
parallel fo cpu_intensive():
result = complex_calculation()
print(f"Calculation complete: {result}")
break
# Parallel declarative block
parallel de batch_processor(100):
process_batch_item(batch_processor.current_iteration)
sleep(0.1)
main:
cpu_intensive.start()
batch_processor.start()
# Main thread remains responsive
handle_user_input()
# Start blocks
my_block.start()
# Stop blocks
my_block.stop()
# Check if block is running
when my_block.status == "RUNNING":
print("Block is active")
de important_work(20):
progress = progress + 1
# Save checkpoint at halfway point
when important_work.current_iteration == 10:
important_work.save()
# Handle error condition
when error_occurred:
important_work.savestop() # Save and stop
fix_error()
important_work.startsave() # Resume from save
# Discard save when no longer needed
when progress >= 20:
important_work.discard()
.save()
: Save current execution state.savestop()
: Save state and stop block.startsave()
: Start from saved state.discard()
: Remove saved stateshared_counter = 0
task_complete = False
fo producer():
shared_counter = shared_counter + 1
when shared_counter >= 100:
task_complete = True
break
fo consumer():
when shared_counter > 0:
process_item(shared_counter)
shared_counter = shared_counter - 1
when task_complete:
break
main:
producer.start()
consumer.start()
events = []
fo event_generator():
events.append(generate_event())
sleep(1)
fo event_processor():
when len(events) > 0:
event = events.pop(0)
handle_event(event)
de robust_processor(10):
success = process_item(robust_processor.current_iteration)
when not success:
print("Processing failed, retrying...")
robust_processor.current_iteration = robust_processor.current_iteration - 1
when success:
print("Item processed successfully")
fo resilient_service():
result = attempt_operation()
when result["error"]:
print(f"Service error: {result['error']}")
sleep(5) # Wait before retry
# Block continues running
when result["success"]:
handle_success(result["data"])
sleep()
in forever blocks to prevent tight loops# Access block properties
print(f"Current iteration: {my_block.current_iteration}")
print(f"Block status: {my_block.status}")
print(f"Has saved state: {my_block.has_saved_state}")
# Conditional logic based on block state
when my_block.status == "COMPLETED":
print("Block finished successfully")
when my_block.status == "STOPPED":
print("Block was stopped manually")