Reserved Keywords in Kiwi¶
This document provides an overview of reserved keywords in Kiwi, along with explanations and usage examples.
Variables¶
var¶
The var keyword is used to declare a variable.
const¶
The const keyword declares an immutable constant. Constant names must be all uppercase with underscores.
Control Structures¶
break¶
The break keyword is used to exit a loop.
See Loops and Control Structures.
do¶
The do keyword is used to define loop and lambda blocks.
else¶
The else keyword is used to define the else branch in a conditional statement.
See Conditionals and Control Structures.
elsif¶
The elsif keyword is used to define an elsif branch in a conditional statement.
See Conditionals and Control Structures.
end¶
The end keyword is used to terminate a code-block.
exit¶
The exit keyword is used to terminate the program. It accepts an integer parameter to represent the exit code.
See Control Structures.
for¶
The for keyword is used to define a for-loop.
See Loops.
case¶
The case keyword is used to define a case statement.
See Conditionals and Control Structures.
if¶
The if keyword is used to define the if branch in a conditional statement.
See Conditionals and Control Structures.
in¶
The in keyword is used to specify the collection to iterate in a for-loop.
See Loops.
next¶
The next keyword is used to skip to the next iteration of a loop.
See Loops and Control Structures.
repeat¶
The repeat keyword is used to define a repeat-loop.
See Loops.
to¶
The to keyword is used in range expressions to define an inclusive range of integers.
See Ranges.
while¶
The while keyword is used to define a while-loop.
See Loops, Conditionals and Control Structures.
when¶
The when keyword is used to define a condition for break, exit, next, return, and throw.
See Conditionals and Control Structures.
Error Handling¶
catch¶
The catch keyword is used to define a catch-block in a try-catch.
See Error Handling.
finally¶
The finally keyword is used to define a finally-block in a try-catch.
See Error Handling.
throw¶
The throw keyword is used for throwing errors.
See Error Handling and Control Structures.
try¶
The try keyword is used to define a try-block in a try-catch.
See Error Handling.
Functions and Methods¶
def¶
The def keyword is an alias for fn.
fn¶
The fn keyword is used to define a function or method.
private¶
The private keyword is used to declare a method with private access (can only be accessed within the struct).
See Structs.
return¶
The return keyword is used to return a value from a method, or to exit a method early.
See Functions and Methods and Control Structures.
static¶
The static keyword is used inside a struct to declare a static method or a static variable.
Static methods can be invoked directly on the struct without an instance.
struct MathUtils
static fn square(n: integer): integer
n * n
end
end
println(MathUtils.square(4)) # 16
Static variables are shared across all instances of a struct. Declare with static @name = value (an optional type hint is supported). Read or write them inside any method using @@name, or externally using StructName.name.
struct Counter
static @count: integer = 0
fn new()
@@count += 1
end
static fn get(): integer
@@count
end
end
Counter.new()
Counter.new()
println(Counter.get()) # 2
println(Counter.count) # 2
Counter.count = 0
println(Counter.count) # 0
See Structs and Static Variables.
with¶
The with keyword is used to define a lambda.
See Lambdas.
yield¶
The yield keyword is used inside a generator function to produce a value and suspend execution until the next value is requested.
See Generators.
Structs¶
abstract¶
Reserved for future use.
struct¶
The struct keyword is used to define a struct.
See Structs.
new¶
The new keyword is used to instantiate a struct.
See Structs.
override¶
Reserved for future use.
@ (self)¶
The @ symbol is used inside a struct method to access or assign instance variables.
struct Point
fn new(x, y)
@x = x
@y = y
end
fn to_string(): string
"(${@x}, ${@y})"
end
end
p = Point.new(3, 4)
println(p.to_string()) # (3, 4)
See Structs.
Packages¶
as¶
The as keyword is used to specify an alias for a package or to specify an iterator variable in a repeat-loop.
export¶
The export keyword is used to export a package to a calling script.
See Packages.
import¶
The import keyword is used to import a script or a package.
See Packages.
include¶
The include keyword executes another Kiwi file inline in the current scope, making its definitions immediately available.
package¶
The package keyword is used to define a package.
See Packages.
require¶
The require keyword loads a package by name if it has not already been imported. Unlike include, it looks up the package by name rather than file path.
Events¶
emit¶
The emit keyword fires a named event, optionally passing arguments to registered handlers.
off¶
The off keyword removes all handlers registered for a named event.
on¶
The on keyword registers a handler that runs every time the named event is emitted.
once¶
The once keyword registers a handler that runs only the first time the named event is emitted.
See Events.
Console I/O¶
input¶
The input keyword is used to request user input from a console.
See Console I/O.
eprint¶
The eprint keyword is used to print to the standard error stream.
See Console I/O.
eprintln¶
The eprintln keyword is used to print to the standard error stream. The output is terminated with a newline.
print¶
The print keyword is used to print output to the standard output stream.
See Console I/O.
println¶
The println keyword is used to print output to the standard output stream. The output is terminated with a newline.
See Console I/O.
printxy¶
The printxy keyword prints output at a specific terminal cursor position.
Miscellaneous¶
delete¶
The delete keyword is used to remove elements from collections.
It can also be used to delete objects.
eval¶
The eval keyword parses and executes a string as Kiwi code.
global¶
The global variable is a hashmap used to share data between scripts.
go¶
The go keyword is used in the Kiwi REPL to execute statements.
null¶
The null keyword is a None value.
See Types.
... (no-op)¶
The ... keyword is used as a placeholder/no-op.
true¶
The true keyword is inverse of false and is a Boolean value.
See Types.
false¶
The false keyword is inverse of true and is a Boolean value.
See Types.