Syntax Catalog
Complete reference of Clean Language syntax and features.
Variables
integer x = 42
number pi = 3.14
string name = "Clean"
boolean active = trueDeclare typed variables with initial values.
print "Hello, World!"
print("With parentheses")Output text to the console. Parentheses are optional.
Comments
// This is a commentSingle-line comments start with //.
Input
string name = input("Enter your name: ")
integer age = input.integer()
boolean confirm = input.yesNo("Continue?")Read input from the user with type-safe methods.
Function Declaration
functions:
integer add(integer a, integer b)
return a + bDeclare functions in a functions: block with return type first.
Function Calls
integer result = add(3, 4)
print result.toString()Call functions by name with arguments.
If/Else
if x > 0
print "positive"
else
print "not positive"Conditional branching without parentheses.
While Loop
while i < 10
print i.toString()
i = i + 1Repeat while condition is true, no parentheses.
Iterate Range
iterate i in 0 to 10
print i.toString()Loop over a numeric range using 'in' keyword.
Iterate Array
iterate item in myArray
print item.toString()Loop over array elements using 'in' keyword.
Class
class Person
string name
integer age
constructor(string n, integer a)
name = n
age = aDefine classes with fields directly under the class declaration. No 'this' keyword - use different parameter names.
Inheritance
class Dog is Animal
string breed
constructor(string n, string b)
base(n)
breed = bExtend classes with 'is' keyword and base() constructor calls.
Arrays
array<integer> nums = [1, 2, 3, 4, 5]
nums.push(6)
integer len = nums.length()Typed arrays with push, length, and indexing. Use lowercase array
Arithmetic
integer sum = a + b
integer diff = a - b
integer prod = a * b
integer quot = a / b
integer rem = a % b
number power = x ^ yBasic arithmetic operators: +, -, *, /, %, ^
Comparison
boolean eq = a == b
boolean neq = a != b
boolean lt = a < b
boolean gt = a > b
boolean lte = a <= b
boolean gte = a >= bComparison operators: ==, !=, <, >, <=, >=
Logical
boolean both = a and b
boolean either = a or b
boolean opposite = not a
boolean check = x is SomeTypeLogical operators: and, or, not, is
String Interpolation
string greeting = "Hello, {name}!"
string result = "Sum: {a + b}"Embed expressions in strings using curly braces.
Methods
string s = "hello world"
s.length()
s.toUpperCase()
s.substring(0, 5)
s.contains("world")
s.replace("world", "Clean")
s.split(" ")
s.trim()Built-in string manipulation methods.
Require
functions:
integer divide(integer a, integer b)
require b != 0
return a / bPrecondition validation with require.
Rules
functions:
integer clamp(integer val, integer min, integer max)
rules
val >= min
val <= max
return valMultiple validation rules.
Later (Await)
later data = start fetchData()
print dataWait for async function result with 'later' and 'start'.
Background (Fire-and-forget)
background logAction()
function syncCache() backgroundExecute async functions without waiting for results.
Import Block
import:
math
utils/stringsImport modules at the start of a file.
Private Functions
private:
void helperFunction()
// internal onlyMark functions as internal-only with private: block.
Tests Block
tests:
"add returns sum": add(2, 3) = 5
"divide by zero fails": divide(10, 0) = errorDefine inline tests with expected values.
Batch Operations
integer:
x = 10
y = 20
print:
"Hello"
"World"
constant:
PI = 3.14159
MAX_SIZE = 100Apply type or function to multiple statements in a block.
Intent
functions:
void processPayment(number amount, string method)
intent "Process a payment transaction"
// function bodyDescribe function purpose in natural language for AI tools.
Spec
functions:
number calculateDiscount(number price, number pct)
spec "specs/pricing/discount.spec.cln"
return price * (pct / 100)Link functions to specification documents for traceability.
Source Block
source:
spec: "specs/payment.spec.cln"
version: "a3f2c1d"Mark files as generated from specifications with version tracking.