Functions and generics
Functions make parameters and results explicit. Local call sites may infer generic arguments, but exported and stored shapes remain statically visible.
For the connected syntax treatment, read Functions and generics in the Language Manual.
Functions and return paths
function greet(name: string): string {
return "Hello, " + name;
}
function log(message: string): void {
console(message);
}A block body with a non-void result must return on every continuing path. Result<T> is a distinct return effect covered in Errors and nullability.
Arrow functions
Arrows support expression and block bodies, function-type annotations, callbacks, and imported Go function values:
const add = (left: int, right: int): int => left + right;
const checked = (value: int): int => { return value; };
const transform: (value: string) => string = (text: string): string => text;Call targets, receiver expressions, and arguments evaluate once in source order at the boundaries where order affects behavior.
You can also use const main = () => { ... }, omit arrow parameter/result annotations when a matching callback type supplies them, and infer block-body results. The Manual's arrow section explains callable declarations, recursion, mutable bindings, and the generated-Go API change.
Rest parameters
Rest parameters use a TypeScript-shaped slice annotation and lower to Go variadics:
function sum(prefix: int, ...values: int[]): int {
let total = prefix;
for (const value of values) { total += value; }
return total;
}
const direct = sum(10, 1, 2);
const values = [3, 4];
const expanded = sum(10, values...);The rest parameter must be last. Inside the function it is a slice. Functions, methods, interfaces, arrows, function types, and constructors use the same rule.
Generic functions
function identity<T>(value: T): T { return value; }
function second<T, U>(left: T, right: U): U { return right; }
const inferred = identity("hello");
const explicit = identity<string>(inferred);
const goShaped = identity[string](explicit);
const partial = second<int>(1, goShaped);Calls may infer all arguments or provide a leading partial/full list with <T> or [T]. Repeated parameter uses must infer the same type, and every uninferred type parameter must be supplied. An uninstantiated generic function cannot be stored as a function value.
Direct arrow callback parameters can also be inferred in native and Go generic calls. See generic callback inference for examples and inference boundaries.
Constraints
extends comparable maps to Go's comparable constraint:
function equal<T extends comparable>(left: T, right: T): boolean {
return left === right;
}Slices, maps, and functions do not satisfy it. Source constraints can also name exact and underlying type sets, for example constraint Integer = int | ~int64. Generic constraints such as constraint Slice<E> = ~E[] support dependent bounds and inference. Constraint declarations are compile-time contracts, not runtime interface values.
constraint Narrow = A & B restricts a type parameter to types shared by both source type sets. Use named intermediate constraints to combine unions and intersections; a single declaration cannot mix | and &.
Ordinary Go interfaces can add method requirements: constraint Printable = ~int & fmt.Stringer. Generic code can then use both integer operators and value.String(). constraint Stream = io.Reader & io.Closer combines method contracts without restricting the underlying type.
Generic named types
Classes, structs, interfaces, and distinct defined types may declare parameters and must be fully instantiated in type positions:
struct Box<T> {
public value: T;
public function get(): T { return this.value; }
}
const box: Box<int> = Box<int> { value: 42 };Methods can use enclosing type parameters and declare their own. A method-local generic is emitted as a typed Go helper with an explicit receiver; it cannot be virtual, override, or final. Generic class inheritance, ordinary virtual/static members on generic classes, and generic aliases are supported. Use type parameter names distinct from the enclosing class or generic method receiver type.
Multiple results
Source callables can declare and forward separate results:
import go { Println } from "fmt"
function pair(): (int, boolean) {
return 7, true
}
function forward(): (int, boolean) {
return pair()
}
function describe(value: int, present: boolean): int {
if (present) { return value + 1 }
return 0
}
const main = () => {
const [value, present] = forward()
Println(value, present, describe(pair()))
}This prints 7 true 8. The sole call argument to describe expands into its two parameters and evaluates pair once. A result list is not a tuple or an array, and cannot be stored as one value. The same signatures work in methods, arrows, interfaces and function types.
Direct Go results use the same local binding and reassignment forms:
const [value, err] = strconv.Atoi(text);
let [next, nextErr] = strconv.Atoi(other);
[next, nextErr] = strconv.Atoi(replacement);No result, including error, is silently discarded. Use _ explicitly when a position is intentionally ignored.