Reference Guide

Syntax Catalog

Complete reference of Clean Language syntax and features.

Variables

integer x = 42
number pi = 3.14
string name = "Clean"
boolean active = true

Declare typed variables with initial values.

Print

print "Hello, World!"
print("With parentheses")

Output text to the console. Parentheses are optional.

Comments

// This is a comment

Single-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 + b

Declare 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 + 1

Repeat 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 = a

Define 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 = b

Extend 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 ^ y

Basic 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 >= b

Comparison operators: ==, !=, <, >, <=, >=

Logical

boolean both = a and b
boolean either = a or b
boolean opposite = not a
boolean check = x is SomeType

Logical 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 / b

Precondition validation with require.

Rules

functions:
	integer clamp(integer val, integer min, integer max)
		rules
			val >= min
			val <= max
		return val

Multiple validation rules.

Later (Await)

later data = start fetchData()
print data

Wait for async function result with 'later' and 'start'.

Background (Fire-and-forget)

background logAction()
function syncCache() background

Execute async functions without waiting for results.

Import Block

import:
	math
	utils/strings

Import modules at the start of a file.

Private Functions

private:
	void helperFunction()
		// internal only

Mark functions as internal-only with private: block.

Tests Block

tests:
	"add returns sum": add(2, 3) = 5
	"divide by zero fails": divide(10, 0) = error

Define inline tests with expected values.

Batch Operations

integer:
	x = 10
	y = 20

print:
	"Hello"
	"World"

constant:
	PI = 3.14159
	MAX_SIZE = 100

Apply type or function to multiple statements in a block.

Intent

functions:
	void processPayment(number amount, string method)
		intent "Process a payment transaction"
		// function body

Describe 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.