Design Decisions
Why Koji is the way it is. Each decision says what Koji does, why, and what was considered instead. The other documents describe how to use the language; this one keeps the reasons, so that later decisions stay consistent with them. How the compiler and the runtime are made is in internals.md.
Variables
mut and immut
Decision: every declaration starts with mut (the variable can change) or immut (neither the variable nor its contents can change). x = 1 only changes an existing mut variable.
Why: the keyword makes the intent visible, and a typo (cuont = 1) is an error instead of a new variable. With value semantics, two levels are enough: changing a field and replacing the whole value are both ways of changing the variable.
Alternatives: var, let and const, as in TypeScript, have a third level (a variable that cannot be replaced but whose contents can change), which means little with value semantics. Declaring by assignment, as in Python, hides typos. var, const and let stay reserved words, so the error points to mut and immut.
for and match variables are immut
Decision: for x in items and the variables of a match pattern are immut; for mut x in items allows changing x (a copy: the list does not change).
Why: changing them is rarely intended, and changing a copy can be mistaken for changing the list.
Types
No any
Decision: there is no any type. Dynamic data is a sum type (json.Json).
Why: every value has a known type at compile time, so no value needs to carry its type when the program runs, and every use of dynamic data is checked.
Alternatives: any with runtime checks, as in TypeScript, moves type errors to runtime.
Nominal types, no interface, no anonymous objects
Decision: type declares every type. Shape types are nominal: two types with the same fields are different types. An object literal is a value of the type expected where it is written, and there is no subtyping.
Why: Celsius and Fahrenheit do not mix, and a value always has one known type. Generic constraints are deduced from the structure of each type (equatable, orderable), and other behavior is passed as a function, so interfaces are not needed.
Alternatives: structural types and interfaces, as in TypeScript and Go. interface stays a reserved word, as a precaution.
int, decimal and float
Decision: an int becomes a decimal or a float only as a single value (a variable, an argument, a field, the items of a list literal), never inside an existing value: an int[] is not a decimal[]. int / int is an int truncated toward zero, and % has the sign of the dividend.
Why: converting a single value is free; converting a list would copy it without the code saying so. Integer division follows Go and Rust.
Numbers are always finite
Decision: there is no NaN and no infinity, in a decimal or a float. A result that does not fit (float(1e308) * float(10), 9223372036854775807 + 1) panics, like a literal that does not fit (0x1p1024, 1e7000) is a compile error. In math, a result that does not exist in the real numbers is an Err(MathError): sqrt(-1) is Complex, pow(0, -1) is Infinite.
Why: NaN spreads silently through computations and breaks equality (NaN != NaN). The rule separates two kinds of problems: a representation limit (the result exists but does not fit: a larger type would hold it) panics, and a domain problem (the result does not exist in the reals) is an error the code can handle. Operators cannot return a Result, so they always panic, including on division by zero. 0 ** 0 is 1, the value of the expression in algebra (only the limit form 0⁰ is indeterminate). MathError.Indeterminate is kept for future functions.
Alternatives: IEEE 754 infinities and NaN, as in most languages.
decimal
Decision: decimal is a number with 34 significant digits in base 10 (the precision of IEEE 754 decimal128), computed by the runtime in software, in 16 bytes, and it is the type of a literal with a point or an exponent (1.5, 2e3): exactly the number written. A literal that does not hold exactly (more than 34 significant digits, or out of range) is a compile error. An operation rounds its exact result to 34 digits, half to even. A decimal is shown in full, as in everyday writing: 10, 1.5, 2500000, without trailing zeros (1.50 equals 1.5); a whole number is shown in full, like an int, up to the 34 digits a decimal holds (beyond them, in scientific notation: 1e300), and a fraction that needs six or more zeros after the point before its first digit is shown in scientific notation, as science writes it (1e-7). It panics when a result does not fit, and also when it is too small to be anything but zero. ** takes any decimal exponent: an int exponent is exact, another is computed with 64 digits. The standard library uses decimals where it does not compute heavily: seconds in time, the numbers of json, math.pi and math.e.
Why: Koji keeps the precision of the program above the influence of the hardware. A float is binary: 0.1 is not one tenth, and 0.1 + 0.2 is 0.30000000000000004 in a float. Precision is not the only reason:
- What is written is what is held. A decimal read from text and written back gives the same text; a float only looks like
0.1because its shortest decimal is shown. - Equality works.
0.1 + 0.2 == 0.3, so comparisons, the keys of a map and the cases of amatchbehave as people expect. - The same result on every machine. All the arithmetic of a decimal,
sqrtandpowincluded, is in the runtime of Koji, and so are the functions of a float (math.sqrt,**): the same code on every system, rather than the system'slibm, whose results differ between systems. - Data stays as it came. Numbers from JSON, a file or the user are kept exactly, and a program that reads and writes them back does not change them.
- What some domains require. Money, taxes and accounting round in base 10, often half to even, by rule.
- Fewer traps. A whole kind of bug, common for beginners, disappears, and each conversion to a float is written in the code, like the rest of what Koji does not hide.
The cost is speed, and a float is there when it matters. 34 digits hold every float exactly (the shortest decimal of a float has at most 17 digits), so converting a float to a decimal and back loses nothing, and a decimal never looks as close as a float would: 16 digits would round 0.30000000000000004 to 0.3. Measured, the common operations stay fast (an addition with the same exponent or an exact product in about 5 ns; a rounded product about 50 ns and a division about 70 ns, runtime/bench/decimal.c): a value is not normalized, so most operations need no division, and it is passed in two registers, never allocated. Half to even does not drift when many results are rounded. Equal values print the same; a number shown in full is the one people read (the exponent of a scientific notation is easy to miss), a whole number reads the same as an int of the same value, and scientific notation takes over for the small fractions science writes that way, where the zeros are only placeholders. A whole number with more than 34 digits would end in zeros the decimal does not keep, so its scientific notation shows exactly what is held, without a line of thousands of digits.
Alternatives: 16 digits (decimal64), which fits in 8 bytes but is about as precise as a float; precision without limit (Java's BigDecimal, Python's Decimal), whose division needs a precision chosen somewhere (in Python, a global context); rational numbers (Raku), exact for the four operations but whose denominators grow without limit; keeping only float.
float is written as it is stored
Decision: float stays the 64-bit binary type of the processor, for complex computations. It is written and shown in base 16, as it is stored: a mantissa after 0x and a power of 2 after p (0x1.999999999999ap-4 is the float nearest to 0.1). A float literal must be exact (at most 53 bits, in range). A decimal literal is not a float: float(0.1) converts it (computed while compiling), and decimal(x) of a float is the shortest decimal that is the same float again, so decimal(float(0.1) + float(0.2)) is 0.30000000000000004. A float and a decimal never mix: price * rate with a decimal and a float is an error, and one of them is converted.
Why: the value a program shows is the value it holds. A float shown in base 10 looks like the decimal it approximates (0.1), hiding the rounding that makes 0.1 + 0.2 != 0.3; in base 16 it is exact and short, and a hexadecimal literal is what C, Java and Go use for exact floats. Converting explicitly shows where a value is rounded. Floats keep the speed of the processor where it matters (simulations, graphics), and the conventions say which type to use.
Alternatives: renaming float to binary and writing it in base 2 (0b1.1p1), exact too but much longer; showing a float as its shortest decimal, as most languages do; letting a decimal literal become a float where a float is expected, which would round without the code saying so.
char
Decision: char is a primitive type, a Unicode code point. A one-character literal is a char where a char is expected.
Why: text processing (a lexer, a parser) works on characters, and a char cannot be confused with a string of any length.
Maps and sets
Decision: Map[K: equatable, V], written ['Ana': 30] ([:] when empty), and Set[T: equatable], from Set.from(list). There are no index signatures.
Why: a dictionary with keys of any equatable type, checked like any other generic type.
Generics
Decision: type parameters in brackets after the name (function first[T], type Pair[A, B]), with the constraints equatable and orderable, which the compiler deduces from the structure of each type. Type arguments are never written at a call: they come from the arguments and the expected type. A generic function used as a value needs an expected function type, which gives its type arguments.
Why: the same bracket syntax as Option[T], and no declarations to keep in sync with what a type can do.
Alternatives: angle brackets, which are ambiguous with comparisons; constraints that types must declare (interfaces, traits).
Ok()
Decision: a Result[void, E] succeeds with Ok(), without an argument; it is shown as Ok(void).
Why: there is no void value to write.
byte
Decision: byte is a primitive type, an unsigned integer of 8 bits (0 to 255), for binary data (byte[]) and memory. An integer literal is a byte where a byte is expected; bytes go with bytes, and convert with byte(n) (which panics outside 0 to 255) and int(b). Arithmetic panics when the result is not a byte, like that of ints; the bits stay in 8 bits.
Why: binary data (files, the network, memory) is made of bytes, and Koji had no type for them: an int is 8 bytes. As char is not a number but a character, byte is a number of its own size.
Alternatives: a byte as an int from 0 to 255 (no type: readByte and writeByte in koji:unsafe, and lists of ints 8 times as large); arithmetic that wraps around (255 + 1 is 0), which hides mistakes, as for ints.
Control flow
No else if
Decision: if has an optional else with a block; there is no else if. More than two branches are a match with guards, or special cases handled first with an early return.
Why: two rules of thumb, built into the language:
- When a condition needs a third branch, it is a case analysis, and a case analysis reads better as
switch/casethan as anif/else if/elsechain. Koji'smatchtakes that role: with guards, it lays the cases side by side (x if (x > 18) => 'Adult'), and with patterns it also checks that every variant is handled. - Early returns should be the natural way to write: special cases first, each ending with
return(orbreakandcontinue), and the main code after them, unnested (see Conventions).else ifinvites the opposite, a chain where every branch stays nested until the end; without it, the early return is the obvious way to handle a special case.
Alternatives: else if, as in most languages; a switch statement next to match, which would be a second construct for the same job. A match without a value (match { x > 18 => ... }) could make guard chains shorter later.
Conditions in parentheses
Decision: conditions are written in parentheses: if (x > 0), while (running), match (value).
Why: the condition is clearly delimited from the block, like the arguments of a call, and not (a and b) reads without doubt about what not applies to.
Errors
No try / catch
Decision: expected failures (a missing file, invalid JSON, int('abc')) are Result values; bugs (an index out of range, a division by zero, an overflow) panic: the program stops, and a panic cannot be caught. panic(message) reports impossible states.
Why: the signature of a function says whether it can fail, and the compiler checks that the error is handled. Exceptions are invisible in signatures, and catching them makes bugs look like expected failures.
?
Decision: x? gives the Ok (or Some) value, or returns the Err (or None) from the function; at the top level of the main file, it stops the program with the message. When the error type differs from the function's, ? wraps it in the one variant of the function's error type that holds exactly that type; if there is no such variant, or several, it is a compile error, and mapErr converts explicitly.
Why: propagating errors stays short without hiding them, and the conversion is automatic only when there is a single possible meaning.
Typed errors in the standard library
Decision: every fallible function of the standard library returns a Result with an error type of its module (FileError, HttpError, JsonError...), and print shows them as readable messages (file 'notes.txt' not found).
Why: the code can match on what went wrong, and the message is ready for the user.
Functions and methods
Methods and self
Decision: a method is a function prefixed with its type, with an explicit self: function Person.greet(self). Without self, it is an associated function, called on the type (Person.create('Ana')). A method that changes its value declares mut self; the call needs no marker. There are no special constructors.
Why: methods are ordinary functions, declared next to the type, and what a method may change is in its signature.
Alternatives: classes, this, constructors: more concepts for the same result.
mut and take parameters
Decision: a parameter the function may change is mut, both in the declaration and at the call (addItem(mut items, 2)). A take parameter takes ownership of a move-only argument. Other parameters are read-only.
Why: with value semantics, a function cannot change the caller's variable unless the call says so, visibly. A mut argument is the variable itself: the function gets its value when the call happens, after the other arguments.
Closures capture a copy
Decision: a closure captures a read-only copy of the variables it uses, from the moment it is created. Top-level variables are not captured.
Why: no hidden shared state between a closure and the code around it.
Anonymous functions take their parameter types from where they are written
Decision: a parameter of an anonymous function written without a type has the one of the function type expected where it is written (task.group((tasks) => ...), immut f: (int) => int = (x) => x * 2), or, given to a generic function, the one its other arguments tell (result.mapErr((e) => ...), which are checked first); with no such type, it needs one.
Why: the type is already written once, in the signature; writing it again at each call is noise, the more so for the functions given to tasks. It is what TypeScript, Kotlin and Swift do.
Alternatives: always writing the types, clearer for a reader without an editor but heavy; inferring them from the body too (as ML), which gives errors far from their cause.
Methods of lists that take a function: lists, not iterators
Decision: lists have map, filter, reduce, any, all and find, which take a function of the items, whose parameters take their types from the list (numbers.filter((x) => x > 0)). Each goes through the list at once and gives a value: map and filter a new list, reduce the value its function builds from an initial one, find an Option. They are written in Koji (koji:core), generic functions that the compiler calls with the list first. filter and find give copies of the items, so a list of move-only values does not have them.
Why: these are what a program does with a list most often after going through it with for, and what a beginner coming from JavaScript, Python or Kotlin looks for; with the types of the parameters taken from the list, they read as short as in those languages. Lists that are made at once keep the cost visible (each step is a list) and need no new concept: no iterator, no laziness, no collect.
Alternatives: lazy iterators (Rust's iter().map().collect(), Java's streams), which save the lists between steps but add a type and a step to learn; functions of a module (list.map(items, f)), longer to read and to chain; comprehensions ([x * 2 for x in items], as Python), a second syntax for loops.
Memory
Ownership and value semantics
Decision: memory is managed with ownership, without a garbage collector. Assigning or passing a value gives an independent copy, which shares the memory until one of the copies changes (copy-on-write, as in Swift). When its owner goes out of scope, a value is dropped. is only tests variants; == compares values.
Why: no aliasing bugs, deterministic memory use, and cheap copies.
Alternatives: a garbage collector with shared references (Java, Go); explicit borrowing with lifetimes (Rust), which is precise but harder to learn. Intentionally shared structures (graphs, caches) keep their values in a list and store indices.
Move-only types and drop
Decision: a type that declares function Type.drop(mut self) is move-only, and so is a type that holds a move-only value (a File). Assigning one moves it; using the previous variable is a compile error.
Why: a resource must be released exactly once; two copies of a file would close it twice. The property follows from the type's contents, so there is no separate marker to keep in sync.
No defer
Decision: there is no defer.
Why: dropping a value releases its resources, at the end of its scope, deterministically. It would be reconsidered only for cleanup that does not belong to any value.
Modules
Mandatory export
Decision: everything in a module is private unless marked with export. An exported mut variable is read-only outside its module, and importers see its current value. A public signature cannot use a private type. Exporting a sum type exports its variants.
Why: a module decides what it offers, and only its own code changes its state.
Imports belong to their block
Decision: an import applies to its whole block, before the other statements (like ES modules); an import inside a block is visible only there, and its module runs the first time the block runs.
Why: declarations can use an import written below them, and a module used only in one place is loaded only if that place runs.
Standard library
File modes
Decision: file.open(path, mode) takes a sum type, Read, Write or Append.
Why: an invalid mode does not compile, unlike a string such as 'rw'.
printError
Decision: a built-in printError(...) writes to the standard error output, with the same arguments as print: the values shown as their display() text, separated by spaces, and a line break.
Why: print and printError are a symmetric pair, and reporting an error is as basic as printing, so it needs no import. The name is in full words, like the rest of the language.
Alternatives: system.printError(...), which needs an import for something as basic as print; values for the standard outputs (file.stderr.write(...)), which would be shared move-only values, and whose write returns a Result that print does not; the abbreviation eprint.
file.remove
Decision: file.remove(path) removes a file and returns a Result[void, FileError]; it does not remove a directory (Err(IsDirectory(path))).
Why: removing a file is the common case, and a separate function for directories (a removeDir, when a program needs it) means a directory is never removed by mistake.
Alternatives: one remove for files and directories.
system.os
Decision: system.os is a constant of the sum type OperatingSystem = { Linux, MacOS, Windows, Other(name: string) }.
Why: a match on it is checked: every system is handled, or there is a _ arm. Other keeps the list open for systems without a variant. It is a constant, like math.pi, since it never changes while a program runs.
Alternatives: a string ('linux'), which a typo ('macOS') gets past the compiler.
system.run and system.capture
Decision: system.run(command, args) runs a program with the terminal of this one (the same input and outputs) and gives its exit code; system.capture(command, args) runs it and keeps what it prints, as an Output.
Why: the two differ in what happens to the program's output, and each name says it: run runs it as if it were typed in the terminal, the common case; capture keeps the output. They are verbs, like the other functions that do something (open, remove, exit), while the nouns of system (args, env, os) only read the state of the process.
Alternatives: system.output(...) for capturing (the name of the type it gives, and of Rust's Command::output), a noun that would hide that it runs a program; keeping run for capturing with a new execute or call for the terminal, names whose difference from run is not visible; a mode argument (run(command, args, Terminal)), which would make the result type depend on the mode.
system.command: a program with its input, directory and environment
Decision: system.command(program, args) gives a Command, a record whose fields say how it runs (input, directory, environment, cleared), changed before it runs; its methods output() and run() run it, as capture and run do (which are command(...).output() and command(...).run()). The input is written while the outputs are read, so that neither side waits for the other, and a program that does not read it does not stop this one (SIGPIPE is ignored while it is written).
Why: the tests of Koji, written in Koji, run the compiler and their programs with an input, in a directory of their own and with variables of their own (KOJI_TARGET, TMPDIR), as any program that drives others does. A record changed field by field needs nothing new of the language, where a builder would need methods for each option, and its defaults are those of capture.
Alternatives: more parameters for capture, every call writing all of them; a builder (command(...).input(...).directory(...), as Rust's Command), a method for each field; a shell (sh -c 'cd dir && ...'), which is not on Windows and must quote everything.
system.executable
Decision: system.executable() gives the path of the executable of the running program, as an Option[string] (None where the system cannot tell it). kojic uses it to find the runtime library next to itself.
Why: the name says what it gives, a file, like path.parent and path.name. It is a noun, like args and env, since it only reads the state of the process; a function, not a constant like system.os, since finding it asks the system.
Alternatives: system.current(), which reads as the current directory, as current_dir in Rust and getcwd in Python; system.programPath(), longer; system.self(), vague.
Regular expressions in a time proportional to the text, look-around included
Decision: regex compiles a pattern for a Pike machine (an automaton that follows every way the pattern can match at once, one thread per state), so a match takes a time proportional to the length of the text, times the size of the pattern, whatever the pattern and the text. Look-around ((?=...), (?!...), (?<=...), (?<!...)) keeps it so: each look-around is a program of its own, answered for every position of the text in one pass over it (forward for a look-behind, backward with its pattern reversed for a look-ahead), the first time it is tried. A look-behind can then have any length. Backreferences ((a)\1) are not supported.
Why: a pattern that takes exponential time on some text (catastrophic backtracking) is a bug that shows only in production, and a way to bring a server down with a crafted input; with this machine no pattern can, so a program can take patterns from its users. Look-around is what people reach for most among what such machines usually leave out (RE2 and Go have none), and the tables give it without losing the guarantee.
Alternatives: backtracking (as Perl, Python, JavaScript and Java), with backreferences and every extension, but exponential in the worst case; look-around run from each position where it is tried, simpler but quadratic; no look-around at all (RE2, Go, Rust's regex).
The epoch of Koji is 0000-01-01
Decision: a timestamp counts the seconds since 0000-01-01 00:00:00 UTC, in the proleptic Gregorian calendar (where the year before 1 is 0, as in ISO 8601): time.now(), datetime.format and datetime.parse use it. A timestamp of Unix (since 1970-01-01), which files, HTTP, most APIs and other languages use, is 62,167,219,200 seconds (719,528 days) less.
Why: the dates of history, from the year 0, have positive timestamps, and the epoch is the start of the calendar rather than an arbitrary date.
Alternatives: the epoch of Unix, which nearly every system uses, so that a timestamp needs no conversion when it is exchanged (the cost of this decision: every exchange adds or subtracts the difference); a date type without a timestamp.
math.abs of several types: the compiler chooses the function
Decision: abs, sqrt, pow, floor, ceil and round of math take an int, a decimal or a float (as they always did). math is written in Koji (std/math.kj), with a private function for each type (absInt, absDecimal, absFloat...); the compiler checks a call by the rules of the function (the same types and messages as a function of Koji: math.pow does not mix a float and a decimal) and calls the function for the types of its arguments, with the Location of the call as its last argument, so that its panics name the line of the program (main.kj:3: integer overflow), not one of std/math.kj.
system.exit is checked by the compiler in the same way, since a function of Koji cannot say that it never returns: it calls leave of std/system.kj, and the code after it is unreachable.
Why: the module moves to Koji without changing for its users, and overloading stays out of the language: six functions do not justify it.
Alternatives: overloading of functions in the language (rules for choosing, for errors and for functions as values, for six functions); generic functions with a constraint for numbers, which Koji does not have; one function for each type in the API (math.absFloat), heavier for every user.
HTTPS: TLS 1.3 only, written in Koji
Decision: https:// uses a client of TLS written in Koji (koji:tls, koji:x509, the cryptography of std/koji/), and only TLS 1.3. Its certificate authorities are those of the system (SSL_CERT_FILE, SSL_CERT_DIR, the usual files of Linux, macOS and the BSDs), with Mozilla's list built in for the systems that have none, as Go does. Its ciphers are AES-GCM with the instructions of the processor (AES-NI and PCLMULQDQ, or those of ARMv8, the only C of the cryptography, in the prelude) when it has them, and ChaCha20-Poly1305 otherwise, as Go, BoringSSL and rustls choose; never AES by tables. The end of the data is close_notify only: a connection closed without it is reported as maybe truncated, as rustls reports it, and std/http.kj ignores that after a body whose size it knows. A server without TLS 1.3 is refused with a message that says so.
Why: TLS 1.3 is what most servers speak, and what has the least to get wrong (no renegotiation, no CBC, no exchange of keys by RSA, the whole handshake after the ServerHello encrypted): one version, a smaller client and a smaller surface of attack. The cost is known: a few servers have only TLS 1.2 (in 2026, registry.npmjs.org and registry.yarnpkg.com), which the client in Rust (ureq) reached and which Koji does not.
Alternatives: TLS 1.2 too, in the modern subset rustls keeps (ECDHE, AEAD ciphers, Extended Master Secret, no renegotiation), which reaches every server but doubles the protocol to keep right; the TLS of the system (OpenSSL, Secure Transport), as Python and Node use it, which ties Koji to a library in C.
The HTTP server: a handler from requests to responses, a task for each connection
Decision: http.serve(port, handler) serves HTTP/1.1: the handler is a function from a Request (method, decoded path, query and headers by lower-case name, body) to a Response (the type the client returns, with its headers), built by hand or by http.text, http.html and http.json. Each connection is a task of the group of serve, kept open for further requests (keep-alive, pipelining) until the client closes it, asks to, or leaves it idle 60 seconds; a request has 30 seconds to come. The server protects itself on its own: 400 for a request with both a length and chunks (smuggling), for white space before the colon of a header and for folded headers; 413, 431, 501 and 505; header values of the handler with a line break have it replaced, so they cannot add headers. It listens on every IPv4 address. A panic in a handler ends the program.
Why: a function from a request to a response is the smallest server to learn, and the whole of what small services need (as Go's http.HandleFunc or Python's WSGI, without the objects); with tasks, the handler is ordinary code that may wait without holding the others up. The protections and the limits are the defaults that a beginner would not know to add.
Alternatives: a router and middleware in the module (as Express), which belong in a library on top; a handler that writes to a stream (Go's ResponseWriter), for streaming bodies, later; recovering from a panic in a handler with a 500 (as Go), which Koji, without catching panics, does not do.
The command line: declarations written as a help shows them
Decision: the module cli declares the options and arguments of a program with the text a help shows for them ('-t, --times <n>', '<name>', '[files...]'), on a command whose methods each return it with one more declaration (cli.command(...).flag(...).argument(...)), and its subcommands; cli.parse reads the arguments, shows the help (made from the declarations, with the fallbacks) for --help, and for a mistake its message, the closest name meant (did you mean '--loud'?) and how to get the help, ending with the code 2. The values are read by name (parsed.flag('loud'), parsed.value('name'), parsed.number('times'), parsed.list('files')); an integer option is checked when the arguments are read. The rules of reading are those of most programs (GNU and POSIX): options anywhere, --name=value, -t3, flags together, --.
Why: a declaration that looks like its help is read at once, also by someone who never saw the module (as commander.js does); everything a small program of the command line needs, the help included, is in one place. Values by name keep it without reflection or macros, which Koji has not.
Alternatives: a record whose fields are the options (as Rust's clap with derive, Python's typer), which needs reflection; a function per kind of declaration with separate short and long names (as Go's flag), more to write; system.args() alone, which leaves the help and the mistakes to each program.
Conventions
Special cases first
Decision: the convention is to handle special cases first, with an early return (or break and continue), so the main code is not nested inside if and else.
Why: the main path reads from top to bottom at one level, and each special case is visible where it ends.
One line break at the end of a file
Decision: a file ends with exactly one line break.
Why: a text file is a sequence of terminated lines, as Unix tools expect: without the final line break, cat joins the last line with the next output and git diff reports it; extra blank lines are noise.
Concurrency
Tasks, and what they may wait for.
Concurrency: light tasks in groups, on one thread
Decision: concurrency is the task module: a task runs a function on a stack of its own (1 MB the system gives as it is used, with a guard page below), and code in a task is ordinary code, with no async or await. In this first phase, the tasks take turns on the one thread of the program: a task runs until it waits (for a timer, the network, another task) or has run long enough, and the runtime (koji:task, written in Koji, with the switch between stacks and the poller of the system in the prelude: epoll on Linux, kqueue on macOS and the BSDs, WSAPoll on Windows) then runs the next one, and waits for the timers and sockets when none is ready. Tasks are structured: each belongs to a group (task.group), which waits for all of its tasks before it returns, so no task outlives the code that started it. Tasks share data through channels (task.channel, with a capacity, 0 for a meeting) and shared values (task.shared, whose update no other task interleaves); everything else a task uses is a copy, as in any closure. A program where every task waits for another stops with a panic that says so.
Why: Koji is for scripts, CLIs, courses and small services: sending requests at the same time, and later serving them, is what they need, and lightweight tasks with sequential code do it without dividing functions into two colors (as async does in JavaScript, Rust and Python), as Go and Erlang show. One thread first means no data races at all, and the runtime stays as it is (counts of references that are not atomic, the caches of the library): code that does not wait is never interrupted. Structured groups (as Swift, Kotlin and Trio have them) make a task's life visible in the code: no task is lost or left running, and an error has a place to go. Shared values cover the simple case (a counter, a cache), channels the coordination of tasks.
Alternatives: async and await (JavaScript, Rust, Python), which color functions; threads of the system (Java before 21, C), heavy and with data races; tasks over several threads at once (Go), the second phase, which needs atomic counts of references and a runtime safe for threads; tasks without groups (Go's go), which can be forgotten.
The value of Shared is the one reference of Koji, which its copies share: the cells of std/task.kj are lists of one item that only sharedGet and sharedSet of koji:unsafe read and change in place. The library is compiled without the shadow stacks of x86-64 (CET), which forbid switching stacks.
No call blocks the other tasks, and a task that computes is preempted
Decision: the calls that block (reading and writing files, directories, input(), waiting for a program, finding the addresses of a host, printing to a pipe that is full or a long text) are made by threads that help, while other tasks are alive: the task waits for its call as for a socket, and the threads run C only, never Koji, taking jobs from a pipe and giving them back through another that the poller watches (no locks). A task alone makes its calls itself; with others alive, print writes at once a text of up to 4 KB to an output that takes it without waiting (poll, which a pipe then takes whole), and the tasks print one at a time, so their texts neither mix nor change order. A task that computes without waiting is preempted: the generated code checks a flag at the end of each turn of a loop and where a function that calls others starts, which a thread sets every 10 ms while several tasks are alive; the task then lets the others run (and the scheduler looks at the sockets and timers), and between two checks, nothing changes for it. Neither the library of the runtime nor the module task has the checks, so their steps stay whole. There is no yield for a program to call.
Why: a task never holds the others up, whatever it does: a server stays responsive with a handler that reads a large file or computes for a second, and nobody has to ask how often to give way, which is the runtime's job, as dropping values is. The checks are the cheapest way to preempt (a load and a branch, lost in the noise of the benchmarks), and they come where the generated code is in a known state, unlike preemption by signals. Threads that only run calls of C keep the runtime without data races (as libuv does for Node). The word yield stays free for generators, which produce values and have nothing to do with tasks.
Alternatives: task.yield() (as Go's runtime.Gosched, Kotlin's yield), which leaves the question to the programmer; preemption by signals at any instruction (Go since 1.14), which needs the stack maps of a garbage collector; asynchronous system calls (io_uring), for Linux only; a resolver of DNS written in Koji (as Go has), which misses what the system configures.
Systems and other languages
Code for one operating system, and the functions of the libraries of the system.
Code for one system: a condition on system.os that the compiler decides
Decision: code that exists only on some systems (the poller is epoll on Linux and kqueue on macOS, a struct of C has other fields on each) is written with system.os as the condition of an if or the value of a match: match (system.os) { Linux => ..., MacOS => ..., _ => ... }. The compiler knows the system it compiles for, so the condition is a constant: the native backend compiles the branch of that system only, and the functions of C that the other branches call are not even linked. Anywhere else, system.os is an ordinary value.
Why: the runtime written in Koji calls the system, whose functions and records differ from one system to another; a test at run time would still link the functions of every system, which do not all exist on any of them. Using the function that already says which system it is keeps the language as it is: no new syntax for a decision of the build.
Alternatives: files for each system (poller_linux.kj, as Go), which duplicates whole modules for small differences; tests at run time with every function of C linked weakly (a missing one is null), which moves the failure from the link to the call; attributes or a preprocessor of Koji's own.
Foreign functions: a block that declares them, with safe types only
Decision: foreign 'library' { ... } declares the functions of a library of the system (its shared library, which Koji's linker links: libsqlite3.so, sqlite3.dll, libsqlite3.dylib for foreign 'sqlite3') and its handles, at the top level of a module. A function takes and returns only values that cross safely, converted at each call: integers (CInt and CSize checked to fit), floats, booleans, bytes, strings (a copy ending with a zero byte; one returned is copied, None for NULL), byte lists (a copy of their bytes), and handles: type Database = handle(sqlite3_close) is an opaque, move-only type whose destructor runs when it is dropped, which only a foreign function can give. A mut parameter is one the function writes. A blocking function runs on a thread that helps while other tasks are alive, and the functions of its block one at a time. The compiler writes, for each block, a module of Koji (compiler/foreign.kj): the function of the library, an extern with the types of the system, and a function of Koji around it that converts and checks, checked like the standard library.
Why: safety by default: what Koji code receives from a library is a copy or a handle it owns, so no pointer into the memory of Koji escapes, and a handle is closed once, at the end of its owner, like a file. The declarations are complete in Koji, defined by the calling conventions of the system rather than by C: they need no header nor C compiler, as Koji depends on no other language, C included. Writing the conversions as Koji, in a module, reuses the checker and the code generator as they are.
Alternatives: checking the declarations against the C headers (which needs a C compiler and ties the language to C; it may come as a tool); a C file compiled with the program for what the safe types do not cover (which ties programs to a C compiler; structs and callbacks will come to the block instead); raw pointers in the language (as Rust's unsafe, or Go's unsafe.Pointer), which a beginner would reach for first.
Tooling
kojic run and kojic build
Decision: kojic build compiles a program to an executable of machine code, linked with a runtime library written in Koji; kojic run compiles it to a temporary executable and runs it. There is no interpreter.
Why: one implementation of the language, so a program behaves the same in development and in production. How the compiler writes and links the machine code is in internals.md.
Alternatives: an interpreter for kojic run, which starts faster but is a second implementation of every feature that must give the same results (Koji had one, in Rust, until the compiler written in Koji replaced it); generating C (Koji did, until its own backend replaced it), which needs a C compiler wherever Koji runs.
Messages name files as they were written
Decision: type errors and panics name the main file as it was given on the command line, and a module as its import wrote it (lib/math.kj:3: division by zero).
Why: the same names everywhere, which the reader recognizes from the code.
kojic fmt: one style, the lines as they were written
Decision: kojic fmt writes code in the one style of Koji, with no options: the spaces between the tokens, the indentation (4 spaces for each bracket open around a line, one more for a line that goes on from the one before), single quotes where a string needs no escape for them, at most one blank line, none after an opening bracket or before a closing one, one around a declaration of several lines at the top level, one space before a comment after code, one line break at the end. The lines stay where the writer put them: a block on one line stays on one, and no line is broken for its length. It works on the tokens, the comments taken from the text between them, and checks what it writes: the same tokens (strings by their values) and the same comments, or the file is left as it is. A file that does not parse is not touched. --check lists the files that are not in the style and fails, and the language server formats too.
Why: one style, which nobody configures, ends the discussions of style (as gofmt did for Go) and makes every program of Koji read the same. Keeping the lines as written keeps the diffs of a change small and leaves the writer the shape of the code: breaking long lines (as Prettier does) moves lines at each small edit. The check of the tokens makes a bug of the formatter harmless: it can leave a file badly formatted, never change what it does.
Alternatives: a printer of the syntax tree, which decides every line (as Prettier and rustfmt), with a width to break at; options for the indentation and the quotes, which bring the discussions back; no formatter, the conventions being a document only.
Tests: test blocks next to the code, one assert
Decision: a test is a block test 'name' { ... } at the top level of a file, next to the code it tests, which it sees whole (private functions and types included). test is a word only there (followed by a string, at the top level): elsewhere it stays a name. A test is checked like a function and left out of a program; kojic test compiles each file with its tests and runs each test in a process of its own, after the top-level statements of the file, showing what a failing test printed. A test fails when it panics. The one function for checking is the built-in assert(condition) or assert(condition, message): the compiler keeps the condition as written, and for a comparison the values of its two sides, shown when it fails (assertion failed: total == 10, left: 9, right: 10); a program's own assert takes its place.
Why: a test is written where its code is, with nothing to import or name (Zig's test blocks, Rust's tests in the same file), and can test what is private. One assert that explains its failure, as pytest does with Python's assert, says as much as a family of assertEqual, assertTrue... with nothing to learn. A process for each test gives isolation without catching panics, which Koji does not do: a test cannot break the next one, and the global state starts again. A contextual word breaks no program that uses test as a name, and the compiler itself does.
Alternatives: test functions in files of their own (math_test.kj, as Go), which see only what is exported; a testing module with test(name, () => { ... }) (as Deno), whose closures could not change the variables around them; several assertion functions (assertEqual, as JUnit or unittest); all the tests of a file in one process, faster but with no isolation after a panic.
Errors show their column and the line they are on
Decision: a compile error gives the file, the line and the column of what is wrong, then that line with it marked: type error: hello.kj:5:13: argument 1 of 'greet' must be string, found int, then 5 | print(greet(42)) and ^^ under 42. The mark is under the value, the argument or the element at fault, not the whole statement (the parser gives each node where it starts and ends). A panic keeps its form, panic: main.kj:12: message: the program running has the line only.
Why: a beginner, and the editor, see at once where the error is, as with gcc, rustc and Elm; file:line:column: is what editors and terminals turn into a link.
Alternatives: the line only, which leaves the reader to find the place in it; a long explanation with each error (as Elm), more work for each message, for later.
The check goes on after an error, statement by statement, and reports every error, up to 20 (3 errors at the end). A variable whose declaration failed is declared all the same, with its written type or an unknown one that no later use reports, so that one mistake gives one error, not a cascade; an imported module with errors stops the check, since every name taken from it would be an error too. Syntax errors stop at the first: the parser cannot know where the program goes on.
The language server is a command of the compiler
Decision: kojic lsp is the language server (the Language Server Protocol, on the standard input and output), written in Koji in the compiler (compiler/lsp.kj): it checks the text the editor holds, saved or not, with the checker of the compiler, and gives every error as it is edited (the line, the column and the width of each), the type of what is under the cursor (from the types the checker kept, even when the file has errors elsewhere) where a name is declared (a variable, a parameter, a function, a type, in the file or in the module it is imported from), the comment right above a declaration as its documentation (with no syntax of its own, as in Go), and the text in the style of kojic fmt. The editor extension of VS Code (koji-vscode, its own repository; the TextMate grammar it copies stays here, in editors/textmate/, for the other editors too) runs it.
Why: the editor sees exactly what kojic check sees, with no second implementation of the language to keep in step; one program to install. Checking a whole file at each change is fast enough for the files of Koji (the compiler checks its own 15,000 lines in 0.08 s).
Alternatives: a server of its own, apart from the compiler (as rust-analyzer), which can be incremental but repeats the language; a server in another language (TypeScript), which would need the compiler anyway.
Packages: repositories of git, tags as versions, minimal version selection
Decision: a directory with a koji.mod is a module (module github.com/ana/agenda), which requires others by the paths of their repositories and the least versions they work with (require github.com/bia/toml v1.4.0); a version is a tag of semver. The versions a program builds with are chosen by minimal version selection: of each module, the greatest of the versions that the program and its packages ask for. From v2, the path of a module ends with its major version (github.com/bia/toml/v2), a module of its own. kojic downloads the modules with git into a cache, and koji.sum keeps the SHA-256 of the files of each version used, checked at each build. A module imports another by its path; the path of a module is the file named as its last part (toml.kj), a path inside it the file of that path. A package with foreign functions is built only when the program allows it (allow foreign); replace takes a module from a directory. kojic init, kojic add and kojic update write koji.mod.
Why: no registry to run or trust: a package is a repository, published by a tag. Minimal version selection gives the same build to everyone without a lock file or a solver, and a release of a dependency changes nothing until the program asks for it (as Go). A major version in the path lets two of them live in one program and keeps an update from crossing an incompatible change. The digests catch a moved tag or a changed copy. Foreign functions are the one way a package can do what Koji cannot check, so the program says which ones it trusts, as the safety by default asks. git is a tool, like the C compiler, not a language: any server it reaches works, private ones included.
Alternatives: a central registry (npm, crates.io), which someone must run, and names that can be taken; ranges of versions solved for the newest (npm, Cargo), which need a lock file to build the same twice; one major version per dependency, simpler to explain, but which blocks a program whose dependencies need two; downloads over HTTPS by the client of Koji, for the hosts whose archives it knows only.