Skip to content

Types and data ​

Kinmokusei preserves Go-compatible representations where they matter. It does not collapse integers into number, erase named types, or hide pointer and collection behavior.

Scalar types ​

  • boolean, string, bstring, and void
  • int, uint, and fixed-width signed/unsigned integers
  • byte and the Go-compatible uint8 alias
  • float32, float64, float, and number
  • complex64 and complex128, with imaginary literals and complex / real / imag

number, float, and float64 are the same type. int remains a separate integer type.

Text and bytes ​

This text-contract split is available in v0.4.6; older compilers have the previous Go byte-string behavior. See migration notes.

string guarantees valid UTF-8 text in checked Kinmokusei code. bstring stores arbitrary immutable bytes and represents strings received from Go. byte[] is mutable byte storage. Both string types lower to Go string, not JavaScript UTF-16. Length and indexing count bytes; for-of decodes int32 code points. A string slice checks UTF-8 boundaries and panics if it splits a code point; a bstring slice permits any in-range byte boundary.

To edit encoded data, use a mutable byte slice via alias Bytes = byte[] and Bytes(text), then validate it back with const text = string(bytes)? inside a Result function. Alternatively split the result and handle its error. Invalid input is rejected, not silently replaced. Use bstring(bytes) when raw bytes are intended, or b"\xff" for a raw literal. For code-point editing, use an int32[] alias instead. Those conversions preserve independence from later slice mutations. Use strconv/fmt for numeric formatting, not string(number), which encodes one code point. Follow the string recipe and detailed string rules.

Collections ​

ts
const values: int[] = [1, 2, 3];
const pair: [2]string = ["hot", "spring"];
const scores = makeMap[string, int]();
scores["hello"] = 42;

Slices alias backing storage as Go slices do. Fixed arrays copy on assignment and parameter passing. Maps are reference-bearing values and have unspecified iteration order.

Objects, structs, and classes ​

  • Structural object types are convenient data-transfer values and generate anonymous Go structs with JSON tags.
  • Native struct declarations are nominal Go-style values whose public fields retain their source names in JSON.
  • class declarations are reference types with identity, constructors, visibility, optional inheritance, and JSON support for public state.

These forms are intentionally distinct. Choose them based on representation and ownership rather than appearance alone.

Anonymous method interfaces ​

Since v0.4.7, method-only anonymous interfaces use structural matching for native classes, structs/pointers, distinct types, named source interfaces and compatible Go values:

ts
function text(value: interface { read(): string; }): string {
  return value.read();
}

Only public, non-generic instance methods count. Pointer-only struct methods require a pointer; a nullable receiver must be narrowed before assignment. Method signatures retain verified text, nullable contracts, Result, multiple results and variadics, including nested method-interface signatures. A raw Go string result cannot promise verified string; an explicit interface conversion does not bypass these checks. nil is accepted as an interface value, but calling a method on a nil interface panics as in Go. Declaring two source methods that lower to the same Go name is a compile error. Passing such a source interface to opaque Go APIs remains conservative when an implementation can hide verified-text storage.

These interfaces can also be generic bounds. This form contains methods only, not properties or embedded interfaces.

Enums, aliases, and defined types ​

Enums are distinct integer types. alias Name = T is transparent. type Name = distinct T creates a nominal defined type with explicit conversions and its own method set.

ts
enum Status: uint16 { Pending, Running = 4, Complete }
alias UserNames = string[];
type UserID = distinct int64;

Since v0.4.7, recursive defined types retain a stable identity while their storage is completed. Map-key and early generic constraints are checked after completion: a recursive pointer or channel can be comparable, but a map/slice cannot be a key merely because it refers to itself. Invalid keys receive a source diagnostic rather than a compiler panic. Direct Go generic arguments that depend on unresolved source storage remain unsupported and are diagnosed; this does not prevent ordinary completed Go generic applications.

Since v0.4.7, enums also support explicit-receiver method expressions:

ts
public function code(this: Status): int { return int(this); }
public function set(this: *Status, next: Status): void { *this = next; }

function example(): int {
  let status = Status.Pending;
  const code = Status.code;       // (Status) => int
  const set = (*Status).set;      // (*Status, Status) => void
  set(&status, Status.Running);
  return code(status);           // 4
}

Value/pointer method sets follow Go rules; Status.set is rejected. Aliases share the enum's method set. Enum constants remain in the original enum namespace: Status.Pending is a constant, not a method expression. If a constant and a method share a name, the constant takes precedence in that namespace; selecting the method through (*Status) or an alias remains possible.

Generics and constraints ​

Functions, structs, interfaces, classes, defined types, and aliases can declare type parameters. Use extends comparable when values must support equality or serve as map keys:

ts
function equal<T extends comparable>(left: T, right: T): boolean {
  return left === right;
}

Standard-library and installed Go-module constraints are available through normal Go imports. The compiler checks both type arguments and which operators the complete Go type set permits:

ts
import go cmp from "cmp";

function minimum<T extends cmp.Ordered>(left: T, right: T): T {
  if (left < right) { return left; }
  return right;
}

cmp.Ordered accepts integers, floating-point values, raw bstring and defined types with those underlying representations. Source constraints can describe type sets directly:

ts
constraint Whole = ~int | ~int64

function addWhole<T extends Whole>(left: T, right: T): T {
  return left + right
}

Exact terms name one type; ~T includes types with that underlying shape. Unions use |, intersections use &, and compatible Go method interfaces can add behavior requirements. These are compile-time bounds, not runtime values. Use ~string in a native constraint for verified text; imported Go string terms have the raw contract. Unbounded parameters must not erase unknown text storage at an opaque Go boundary; use a provable raw/numeric bound there. Every operation in generic code must be valid for the whole set. See constraints for dependent bounds, method contracts and inference rules.

Kinmokusei is a pre-1.0 project. Documentation describes implemented behavior.