---
title: "Rule 8.17: Functions and entry points in TypeShade"
description: "Rule 8.17 of the TypeShade language design, in Functions and entry points."
url: https://typeshade.dev/reference/rules/8-17/
language: en
---

# Rule 8.17

Chapter 8, Functions and entry points Test

A local function (a `const` that holds an arrow function or a function expression, or a `function` declaration, written inside a function’s body) is a function of the module named after the body that declares it (surface §14), and it may read and write the variables of the functions around it, as a TypeScript closure does. A name its body reads is looked up as TypeScript looks it up, from the innermost block outward; one that lands on a `let`, a `const` or a parameter of a function around it is a capture, which the emitted function takes as a parameter ahead of its own and which every call of it passes: by value while neither it nor a local function it calls writes the variable, and by reference once one does; a `const` keeps its constant, so a loop it bounds is still counted ([Rule 7.5](https://typeshade.dev/reference/rules/7-5/)). A whole write to a captured value parameter shares the local copy of [Rule 8.8](https://typeshade.dev/reference/rules/8-8/) with the enclosing body and every other closure; its reference never reaches the enclosing caller. An object parameter that its function writes is captured as the caller’s object it names ([Rule 8.25](https://typeshade.dev/reference/rules/8-25/)): by value while it is only read, by reference once it is written, and handed on as itself; a variable a local function writes counts as a reference in Rule 8.25’s check of the call that passes it. `this` in an arrow function is the object of the method around it, captured the same way, and the class the call names in a static member ([Rule 8.13](https://typeshade.dev/reference/rules/8-13/)). A write through a capture follows the variable’s own declaration: a `let` may be written, a `const` only through what it holds and only when its initializer built the value ([Rule 6.10](https://typeshade.dev/reference/rules/6-10/)), and a whole-rebound value parameter through its mutable local copy (Rule 8.8). A `function` declaration may be called anywhere in its block, as TypeScript hoists it, and a local function may be declared in a generic function, once for each of its instances. A call of a local function at a point where a variable it captures is not declared yet must be refused, since TypeScript throws there; so must a function named where a value is read (held in a variable, returned, compared or chosen at run time), there being no function value, other than as an argument of a parameter that takes a function ([Rule 8.18](https://typeshade.dev/reference/rules/8-18/)). A function named as the callback of a fold (`any(xs, isBig)`, `zip(xs, ys, f)`) passes what it captures to every call the fold makes.

The rule text and the parts under it are the compiler's own English, as the design document writes them.

## Rationale

a function is no value here: nothing can return one, store one or choose one at run time, so every call of a local function is written inside the scope that declares it, where every variable it reads is in scope too. Passing those variables at each call is all a closure’s environment holds, and passing a written one by reference is what makes the write visible to the function around it and to the next call, as it is in TypeScript. A call is where TypeScript would read a variable that is not declared yet, so that is where the refusal stands. Before this rule a local function that read a name from the function around it was refused, with the parameter to add instead.

## Derives from

ECMAScript [function environment records](https://tc39.es/ecma262/#sec-function-environment-records) (a closure reads the binding, not a copy of it), [FunctionDeclarationInstantiation](https://tc39.es/ecma262/#sec-functiondeclarationinstantiation) (a `function` declaration is instantiated before its block runs), [`let` and `const` declarations](https://tc39.es/ecma262/#sec-let-and-const-declarations) (a binding may not be read before its declaration is evaluated) and [arrow functions](https://tc39.es/ecma262/#sec-arrow-function-definitions) (`this` is the enclosing one); [Rule 6.10](https://typeshade.dev/reference/rules/6-10/); [Rule 8.8](https://typeshade.dev/reference/rules/8-8/); [Rule 8.10](https://typeshade.dev/reference/rules/8-10/); [Rule 8.13](https://typeshade.dev/reference/rules/8-13/); surface §14.

## How it is verified

Checked by a test. A test, a gate script or a CI workflow names this rule, and the traceability check fails when a file listed below stops naming it.

Where the rule says the compiler enforces it:

`TS8022 UNKNOWN_NAME` for a call before a captured variable’s declaration (`"f" reads "y", which is not declared yet where "f" is called: a let or a const is not there before its declaration, and TypeScript throws. Call "f" after "y" is declared.`), [`TS8099`](https://typeshade.dev/reference/errors/ts8099/) for a function named as a value (`"f" is a function, and a shader has no function values: nothing at run time can hold one, return one or choose between two. Call it where its value is needed, "f(...)".`), `TS8018 ASSIGN_TARGET` and `TS8005 CONST_ASSIGN` for a write through a parameter or a `const`; pinned by `src/compiler/ts/closures.test.ts`, which holds WGSL, GLSL ES 3.00, the CPU oracle, the codegen and the debugger to one value for each form; `examples/gallery/closures/closures.shade.ts` in the compile gate.

The files that verify it at commit af54ddad5, each at the first line that names the rule:

- [`examples/shade-examples.test.ts:27`](https://github.com/typeshade/typeshade/blob/af54ddad5/examples/shade-examples.test.ts#L27)
- [`src/compiler/ts/closures.test.ts:41`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/closures.test.ts#L41)
- [`src/compiler/ts/mutable-parameters.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/mutable-parameters.test.ts#L1)
- [`src/compiler/ts/reference-parameters.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/reference-parameters.test.ts#L1)

## Explained in

The sections of the surface document that explain this rule, at commit af54ddad5:

- [§4 What we will not do](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/use-typeshade-surface.md#4-what-we-will-not-do)
- [§14 TypeScript shapes the parser already had](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/use-typeshade-surface.md#14-typescript-shapes-the-parser-already-had)
- [§27 Boolean vectors](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/use-typeshade-surface.md#27-boolean-vectors)

## Error codes that enforce it

The diagnostic codes the rule names under Enforced by, or whose registry text names the rule:

**[`TS8005`](https://typeshade.dev/reference/errors/ts8005/) `CONST_ASSIGN`**

An assignment to a name or a place that cannot change: a `const` number or vector, a component of a `const` vector or matrix, a `readonly` field or static, or a storage binding that `readonly` or `"read"` promises is never written. A write to a field or an element of a `const` object is not one of these, because the `const` holds the object and the object stays writable, as in JavaScript ([Rule 6.10](https://typeshade.dev/reference/rules/6-10/)).

**[`TS8018`](https://typeshade.dev/reference/errors/ts8018/) `ASSIGN_TARGET`**

An assignment or `++`/`--` target that is not a writable place, such as a temporary. An unknown name is `UNKNOWN_NAME`, and a write to a known immutable binding is `CONST_ASSIGN`.

**[`TS8022`](https://typeshade.dev/reference/errors/ts8022/) `UNKNOWN_NAME`**

A reference to a name that TypeShade cannot resolve and that is not a function call (`UNKNOWN_FN`) or a type name (`UNKNOWN_TYPE`). A name that the library declares for TypeScript's own use (`Object`, `Math`, `Symbol`), or that the file declares as an enum, a namespace, a class or a type, and that is read as a value, says what it is.

**[`TS8099`](https://typeshade.dev/reference/errors/ts8099/) `UNSUPPORTED`**

The catch-all for a diagnostic whose site does not yet deserve its own code. It stays parked at TS8099, past the sequential range.

## See also

- [Rule 8.16](https://typeshade.dev/reference/rules/8-16/) Test
- [Rule 8.18](https://typeshade.dev/reference/rules/8-18/) Test
- [Rule 6.10](https://typeshade.dev/reference/rules/6-10/)
- [Rule 7.5](https://typeshade.dev/reference/rules/7-5/)
- [Rule 8.8](https://typeshade.dev/reference/rules/8-8/)
- [Rule 8.10](https://typeshade.dev/reference/rules/8-10/)
- [Rule 8.13](https://typeshade.dev/reference/rules/8-13/)
- [Rule 8.25](https://typeshade.dev/reference/rules/8-25/)
- [Design rules](https://typeshade.dev/reference/rules/)
- [Error codes](https://typeshade.dev/reference/errors/)

## Source

The rule at commit af54ddad5:

- [`docs/language-design.md:984`](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/language-design.md#L984) (the design document)
- [`reqs/rules/RULE-0817.md`](https://github.com/typeshade/typeshade/blob/af54ddad5/reqs/rules/RULE-0817.md) (its traceability item)
