Cella Documentation 0.1.0-dev Home Source

Keywords

Cella has two kinds of keywords. Reserved keywords mean the same thing everywhere and can never be used as names. Contextual keywords have a meaning only in certain places, such as at the start of a declaration, and are ordinary names everywhere else.

Reserved keywords

A reserved keyword can't name a variable, parameter, function, type, field or case:

main: fun{	val match = 3'match' is a keyword}

Values

KeywordMeaningChapter
true, falseThe two bool values.Bool, char and strings
nullThe null pointer, for ptr, ptr[T] and cstr; T: null constrains a type parameter.Pointers, Generics
undefA value left uninitialized on purpose: var buffer: array[u8, 64] = undef.Variables
sizeOfThe size of a type or value in bytes, as a usize.Pointers
alignOfThe alignment of a type or value in bytes, as a usize.Pointers
nameOfThe name of a declaration as a string: nameOf(Point).Bool, char and strings
selfThe value a method was called on.Methods

Declarations and bindings

KeywordMeaningChapter
funDeclares a function (area: fun(...)), starts a lambda (fun(x) => x * 2) and names function types (fun[i32 -> i32]).Functions, Closures
valDeclares a binding that can't be reassigned; also a read-only static field and a pattern binding.Variables
varDeclares a binding that can be reassigned; also a writable static field and a pattern binding.Variables
constDeclares a compile-time constant; also marks constant functions (const fun, const op), constant accessors (const get, const set, const prop), value parameters (const length: usize), constant implementations (impl const Hash) and constant bounds (T: const Hash).Variables, Constant functions, Generics
newNames constructors (new: op(...)) and explicit conversions (new: op(self) -> T); T: new(...) constrains a type parameter.Records, Conversions
dropNames destructors (drop: op), destroys a value early (drop x, drop *p); T: drop constrains a type parameter.Ownership
defaultRestores zeroed construction (new: op() = default); T: default constrains a type parameter.Records, Generics
refDeclares a record that holds borrows (ref rec) and names function types that may borrow (ref fun[...]).Borrows, Closures

Control flow

KeywordMeaningChapter
if, elseConditional statements; else if chains them.Control flow
whileLoops while a condition holds.Control flow
doRuns a body once before checking its while condition.Control flow
loopLoops until a break.Control flow
forLoops over a range, an array or a type with loop operators; for: op declares a loop operator.Control flow, Operators
inSeparates a for loop's variable from what it walks; in: op declares a loop operator.Control flow, Operators
breakLeaves a loop, optionally a labeled one.Control flow
contStarts a loop's next iteration, optionally a labeled one.Control flow
retReturns from a function, with or without a value.Functions
matchPicks an arm by matching a value against patterns.Pattern matching
isTests a value against a pattern: shape is circle(_).Pattern matching
whenCompiles code only when a flag condition holds.Conditional compilation
panicStops the program with a message: panic "bad index"; panic: op declares a module's panic operator.Checks and panics

Modes and memory

KeywordMeaningChapter
immBorrows without writing: imm parameters and arguments, imm[T] borrow types, for imm x in xs.Parameter modes, Borrows
mutBorrows for writing: mut parameters and arguments, mut[T] borrow types, for mut x in xs, match mut.Parameter modes, Borrows
ownMoves a value where a plain use wouldn't (own *p, own slot out of a mut parameter, own self.field), and declares types whose values only move (own rec, own enum) and closures that own their captures (own fun).Ownership, Pointers
dynNames trait object types: dyn[Shape].Trait objects
atomicStarts an atomic load, store, update or fence; T: atomic constrains a type parameter.Atomics

Contextual keywords

These words are keywords only where the grammar expects them. Anywhere else they are ordinary names:

main: fun{	val set = 3	val copy = set * 2	printf("%d %d\n", set, copy)}
Output
3 6
KeywordMeaningChapter
modStarts a file's mod line, makes a declaration visible to its module only, and starts a path from the root module (mod.geometry.area).Modules
useImports names from other modules; use when compiles a whole file only when a flag condition holds.Modules, Conditional compilation
pubExports a declaration from its project.Modules
pvtMakes a declaration visible to its file, or a member visible to its type.Modules, Methods
extDeclares C functions and globals (ext { }), C function types (ext fun[...]), C-compatible enums (ext enum), C names (ext("name")); T: ext constrains a type parameter.Calling C
recDeclares a record type.Records
enumDeclares an enum type; T: enum and T: enum(Tag) constrain a type parameter.Enums, Generics
unionDeclares a union type.Unions
traitDeclares a trait; K: trait makes a parameter that takes a trait.Traits, Trait objects
implImplements traits for a type (rec impl Shape, Circle: impl Shape) and adds members in impl blocks.Traits
opDeclares operators, constructors, destructors and conversions: +: op(...), new: op(...), drop: op.Operators
asDeclares an implicit conversion: as: op(self) -> T.Conversions
reqMarks fields that every construction must set, and lists the traits and constraints a trait requires (Shape: trait req Named).Records, Traits
propDeclares a property with get and set accessors.Methods
get, setDeclare property accessors; pvt set and mod set limit who can write a field.Methods
copyConstrains a type parameter to types that can be copied: T: copy.Generics
norefConstrains a type parameter to types that hold no borrows: T: noref.Generics

Memory orderings

After atomic, the words relaxed, acquire and release, and the pair acquire release, choose the memory ordering. Without one, the operation is sequentially consistent. Anywhere else these words are ordinary names:

main: fun{	var counter = 0	atomic relaxed *@counter += 5	val release = atomic acquire *@counter	printf("%d\n", release)}
Output
5

Atomics explains what each ordering guarantees.

Built-in names

The names of the built-in types (i32, str, ptr, array and the rest, listed in Built-in types) and Self aren't keywords, but nothing can be declared with them:

main: fun{	val str = 5Cannot use 'str' as a name}