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
| Keyword | Meaning | Chapter |
|---|---|---|
true, false | The two bool values. | Bool, char and strings |
null | The null pointer, for ptr, ptr[T] and cstr; T: null constrains a type parameter. | Pointers, Generics |
undef | A value left uninitialized on purpose: var buffer: array[u8, 64] = undef. | Variables |
sizeOf | The size of a type or value in bytes, as a usize. | Pointers |
alignOf | The alignment of a type or value in bytes, as a usize. | Pointers |
nameOf | The name of a declaration as a string: nameOf(Point). | Bool, char and strings |
self | The value a method was called on. | Methods |
Declarations and bindings
| Keyword | Meaning | Chapter |
|---|---|---|
fun | Declares a function (area: fun(...)), starts a lambda (fun(x) => x * 2) and names function types (fun[i32 -> i32]). | Functions, Closures |
val | Declares a binding that can't be reassigned; also a read-only static field and a pattern binding. | Variables |
var | Declares a binding that can be reassigned; also a writable static field and a pattern binding. | Variables |
const | Declares 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 |
new | Names constructors (new: op(...)) and explicit conversions (new: op(self) -> T); T: new(...) constrains a type parameter. | Records, Conversions |
drop | Names destructors (drop: op), destroys a value early (drop x, drop *p); T: drop constrains a type parameter. | Ownership |
default | Restores zeroed construction (new: op() = default); T: default constrains a type parameter. | Records, Generics |
ref | Declares a record that holds borrows (ref rec) and names function types that may borrow (ref fun[...]). | Borrows, Closures |
Control flow
| Keyword | Meaning | Chapter |
|---|---|---|
if, else | Conditional statements; else if chains them. | Control flow |
while | Loops while a condition holds. | Control flow |
do | Runs a body once before checking its while condition. | Control flow |
loop | Loops until a break. | Control flow |
for | Loops over a range, an array or a type with loop operators; for: op declares a loop operator. | Control flow, Operators |
in | Separates a for loop's variable from what it walks; in: op declares a loop operator. | Control flow, Operators |
break | Leaves a loop, optionally a labeled one. | Control flow |
cont | Starts a loop's next iteration, optionally a labeled one. | Control flow |
ret | Returns from a function, with or without a value. | Functions |
match | Picks an arm by matching a value against patterns. | Pattern matching |
is | Tests a value against a pattern: shape is circle(_). | Pattern matching |
when | Compiles code only when a flag condition holds. | Conditional compilation |
panic | Stops the program with a message: panic "bad index"; panic: op declares a module's panic operator. | Checks and panics |
Modes and memory
| Keyword | Meaning | Chapter |
|---|---|---|
imm | Borrows without writing: imm parameters and arguments, imm[T] borrow types, for imm x in xs. | Parameter modes, Borrows |
mut | Borrows for writing: mut parameters and arguments, mut[T] borrow types, for mut x in xs, match mut. | Parameter modes, Borrows |
own | Moves 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 |
dyn | Names trait object types: dyn[Shape]. | Trait objects |
atomic | Starts 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)}3 6
| Keyword | Meaning | Chapter |
|---|---|---|
mod | Starts 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 |
use | Imports names from other modules; use when compiles a whole file only when a flag condition holds. | Modules, Conditional compilation |
pub | Exports a declaration from its project. | Modules |
pvt | Makes a declaration visible to its file, or a member visible to its type. | Modules, Methods |
ext | Declares 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 |
rec | Declares a record type. | Records |
enum | Declares an enum type; T: enum and T: enum(Tag) constrain a type parameter. | Enums, Generics |
union | Declares a union type. | Unions |
trait | Declares a trait; K: trait makes a parameter that takes a trait. | Traits, Trait objects |
impl | Implements traits for a type (rec impl Shape, Circle: impl Shape) and adds members in impl blocks. | Traits |
op | Declares operators, constructors, destructors and conversions: +: op(...), new: op(...), drop: op. | Operators |
as | Declares an implicit conversion: as: op(self) -> T. | Conversions |
req | Marks fields that every construction must set, and lists the traits and constraints a trait requires (Shape: trait req Named). | Records, Traits |
prop | Declares a property with get and set accessors. | Methods |
get, set | Declare property accessors; pvt set and mod set limit who can write a field. | Methods |
copy | Constrains a type parameter to types that can be copied: T: copy. | Generics |
noref | Constrains 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)}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}
Cella Documentation