Skip to content

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.

See Variables and var.

const

The const keyword declares an immutable constant. Constant names must be all uppercase with underscores.

const MAX_RETRIES = 3
const API_URL = "https://example.com"

println(MAX_RETRIES)  # 3

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.

See Lambdas and Loops.

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.

println [1 to 5]    # [1, 2, 3, 4, 5]
println [10 to 7]   # [10, 9, 8, 7]

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.

See Functions and Methods.

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.

See Packages and Loops.

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.

include "lib/helpers"

# functions and variables from helpers.kiwi are now in scope

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.

require "xml"

doc = xml::parse("<root><item>hello</item></root>")

Events

emit

The emit keyword fires a named event, optionally passing arguments to registered handlers.

emit "user.login", { name: "Scotty" }

off

The off keyword removes all handlers registered for a named event.

off "user.login"

on

The on keyword registers a handler that runs every time the named event is emitted.

on "user.login" with (user) do
  println("Welcome, ${user.name}!")
end

once

The once keyword registers a handler that runs only the first time the named event is emitted.

once "app.start" do
  println("Application started.")
end

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.

printxy(10, 5, "Hello!")  # prints "Hello!" at column 10, row 5

Miscellaneous

delete

The delete keyword is used to remove elements from collections.

It can also be used to delete objects.

See Hashmaps and Lists.

eval

The eval keyword parses and executes a string as Kiwi code.

eval 'println("hello, world!")'  # prints: hello, world!

code = "1 + 2"
println(eval code)  # 3

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.