Skip to main content
Klar

Keywords

Reserved keywords in Klar that cannot be used as identifiers.

Declaration Keywords

KeywordDescriptionExample
letImmutable variablelet x: i32 = 42
varMutable variablevar count: i32 = 0
fnFunction definitionfn add(a: i32, b: i32) -> i32
structStruct definitionstruct Point { x: i32, y: i32 }
enumEnum definitionenum Color { Red, Green, Blue }
traitTrait definitiontrait Printable { fn print(self: Self) }
implImplementation blockimpl Point: Printable { ... }
typeType aliastype Item = i32
constConstantconst MAX: i32 = 100

Control Flow Keywords

KeywordDescriptionExample
ifConditionalif x > 0 { ... }
elseAlternative branchif x > 0 { ... } else { ... }
matchPattern matchingmatch value { ... }
forFor loopfor i: i32 in 0..10 { ... }
whileWhile loopwhile x > 0 { ... }
loopInfinite looploop { ... break }
breakExit loopbreak
continueSkip iterationcontinue
returnReturn from functionreturn value
inIterator bindingfor x: i32 in list

Type Keywords

KeywordDescriptionExample
i8, i16, i32, i64Signed integerslet x: i32 = 42
u8, u16, u32, u64Unsigned integerslet x: u32 = 42
f32, f64Floating pointlet x: f64 = 3.14
boolBooleanlet x: bool = true
charCharacterlet x: char = 'a'
stringStringlet x: string = "hello"
voidNo valuefn print(s: string)
SelfCurrent type in traitfn clone(self: Self) -> Self

Literal Keywords

KeywordDescriptionExample
trueBoolean truelet x: bool = true
falseBoolean falselet x: bool = false
NoneOptional nonelet x: ?i32 = None
SomeOptional somelet x: ?i32 = Some(42)
OkResult successlet x: Result#[i32, E] = Ok(42)
ErrResult errorlet x: Result#[T, string] = Err("fail")

Logical Operators

KeywordDescriptionExample
andLogical ANDa and b
orLogical ORa or b
notLogical NOTnot a

Reference Keywords

KeywordDescriptionExample
refRead-only referencefn f(ref x: Data)
inoutMutable referencefn f(inout x: Data)
selfInstance in methodfn f(self: Self)

Module Keywords

KeywordDescriptionExample
importImport moduleimport utils.{ add }
pubPublic visibilitypub fn api() -> void { }
asAliasimport math.{ add as sum }

FFI Keywords

KeywordDescriptionExample
externExternal C declarationextern { fn puts(s: CStr) -> i32 }
unsafeUnsafe code blockunsafe { c_function() }
outOut parameter modifierfn get_result(out ptr: CPtr#[i32])

Comptime Keywords

KeywordDescriptionExample
comptimeCompile-time parameterfn f#[comptime N: i32]()

Smart Pointer Keywords

KeywordDescriptionExample
RcReference countedRc.new(value)
ArcAtomic ref countedArc.new(value)
CellInterior mutabilityCell.new(value)

FFI Types

KeywordDescriptionExample
CPtrNon-null C pointerCPtr#[i32]
COptPtrNullable C pointerCOptPtr#[i32]
CStrBorrowed C stringmsg.as_cstr()
CStrOwnedOwned C stringmsg.to_cstr()

Collection Types

KeywordDescriptionExample
ListDynamic arrayList.new#[i32]()
MapHash mapMap.new#[K, V]()
SetHash setSet.new#[T]()
RangeRange type0..10

Result/Option Types

KeywordDescriptionExample
ResultResult typeResult#[T, E]
?Optional type?T (same as Option#[T])

Async Keywords

KeywordDescriptionExample
asyncAsynchronous function modifierasync fn fetch() -> Future#[i32] { ... }
awaitAwait async result inside async functionslet x: i32 = await task

Meta Layer Keywords

KeywordDescriptionExample
metaCode metadata and annotationsmeta intent("Parse expressions")

Reserved for Future Use

The following keywords are reserved and may be used in future versions:

KeywordPotential Use
yieldGenerator functions
macroMacro definitions
whereAdditional trait bounds
dynDynamic dispatch
moveMove semantics
mutMutability (alternative to var)
staticStatic variables
unionUnion types

Contextual Keywords

Some identifiers have special meaning in specific contexts but can be used as regular identifiers elsewhere:

KeywordContextDescription
_Pattern matchingWildcard pattern
selfMethodsInstance reference
SelfTraits/implCurrent type

Identifier Rules

Valid identifiers in Klar:

  • Must start with a letter or underscore
  • Can contain letters, digits, and underscores
  • Cannot be a keyword
  • Case-sensitive
// Valid identifiers
let myVariable: i32 = 1
let _private: i32 = 2
let camelCase: i32 = 3
let snake_case: i32 = 4
let PascalCase: i32 = 5
let x1: i32 = 6

// Invalid identifiers
// let 1x: i32 = 1       // Cannot start with digit
// let let: i32 = 1      // Cannot use keyword
// let my-var: i32 = 1   // Cannot use hyphen

Next Steps