Annotations

In kdts, compiler directives are provided via satisfies expressions, or when it's more convenient, through the @satisfies {} jsdoc tags.

Overridable

Constant variable declarations can be annotated as Overridable. For such variables, the default initializer can be overridden with values supplied from the command line:

worker.ts
import { Overridable } from "@kimlikdao/kdts";

const Status = 404 satisfies Overridable;
const HostUrl = "https://example.com" satisfies Overridable;

export default () => Response.redirect(HostUrl, Status);

When compiled with kdts worker.ts --override HostUrl="test.com" --override Status=405, we get

worker.out.js
export default()=>Response.redirect("test.com",405);

Function classes

In kdts, to achieve better optimizations, functions can be annotated as belonging to certain function classes. For example:

/** @satisfies {PureFn} */
const greet = (name: string) => `Hi, ${name}!`;

Here, greet is annotated as being PureFn , which promises to the compiler that the function has no side effects, does not depend on mutable external state (hence deterministic) and returns a fresh value (we will introduce this formally later; primitives are always fresh).

If the function depended on external mutable state but is side-effect free, we can use the weaker marker SideEffectFreeFn:

An InlineFn is inlined to each call site and the function body is compiled away completely:

Now, each time ensureArray(x) is called, we will see the expression Array.isArray(x) ? x : [x] inlined, unless kdts can prove x to be an array or not an array from the declared or inferred types. In such cases, the function call is compiled to x or [x] .

FreshValue

Before we go over all function classes, let us define FreshValue first. A FreshValue is defined recursively as a primitive (such as string, number, bigint etc) or a fresh object containing FreshValue s. For instance the following are all FreshValues

The following is not a FreshValue

since person is not a fresh object.

List of function classes

Here is the full list of function annotations kdts currently optimizes with:

Annotation
Meaning

DeterministicFn

A function which doesn't read external mutable state

SideEffectFreeFn

A function which doesn't change its argument or any external state

MethodFn

A class method which can only change state reachable from `this`. Further, it cannot read external mutable state.

InPlaceFn

A function which mutates only state reachable from its arguments. Further, it cannot read external mutable state.

InPlaceRandFn

A function which mutates only state reachable from its arguments. It can read external mutable state.

PureAliasFn

A function which doesn't read external mutable state and is side-effect free.

PureFn

A PureAliasFn which also returns a FreshValue

InlineFn

A function which will be inlined to each call site and the function body will be compiled away.

NoInlineFn

A function which cannot be inlined and calls to it must be preserved as-is.

InlineFriendlyFn

A function the compiler is encouraged to inline when profitable, but unlike InlineFn it is not required to inline every call site.

Examples

LargeConstant

A constant variable declaration can be marked as a large constant; this will prevent it from being inlined to each use of it.

PureExpr

Marks an expression as side-effect free and deterministic.

Now kdts is free to eliminate the entire initializer expression of x. If `KeepConsole=true`, kdts is free to replace x with the evaluated result in console.log(x).

Last updated