---
title: "규칙 8.17: 함수와 진입점, TypeShade 셰이더 언어 설계 규칙과 그 근거, 출처, 검증 방식"
description: "TypeShade 언어 설계 규칙 8.17(함수와 진입점)입니다. A local function (a const that holds an arrow function or a function expression…"
url: https://typeshade.dev/ko/reference/rules/8-17/
language: ko
---

# 규칙 8.17

8장, 함수와 진입점 테스트

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/ko/reference/rules/7-5/)). A whole write to a captured value parameter shares the local copy of [Rule 8.8](https://typeshade.dev/ko/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/ko/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/ko/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/ko/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/ko/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.

규칙 본문과 그 아래 항목은 설계 문서에 적힌 컴파일러 원문이라 한국어 페이지에서도 영어로 둡니다.

## 근거

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.

## 출처

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/ko/reference/rules/6-10/); [Rule 8.8](https://typeshade.dev/ko/reference/rules/8-8/); [Rule 8.10](https://typeshade.dev/ko/reference/rules/8-10/); [Rule 8.13](https://typeshade.dev/ko/reference/rules/8-13/); surface §14.

## 검증 방식

테스트로 확인합니다. 테스트나 게이트 스크립트, CI 워크플로가 이 규칙을 가리키고, 아래 파일 가운데 하나라도 규칙을 더는 가리키지 않으면 추적 검사가 실패합니다.

컴파일러가 이 규칙을 적용하는 곳을 규칙이 직접 적은 내용입니다.

`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/ko/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.

커밋 af54ddad5에서 이 규칙을 확인하는 파일입니다. 파일마다 규칙을 처음 가리키는 줄로 연결했습니다.

- [`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)

## 설명하는 절

커밋 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)

## 적용하는 오류 코드

규칙이 Enforced by 항목에서 가리키는 진단 코드와, 레지스트리 설명에서 이 규칙을 가리키는 진단 코드입니다.

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

바뀔 수 없는 이름이나 자리에 값을 대입합니다. `const`에 담은 수나 벡터, `const` 벡터나 행렬의 성분, `readonly` 필드나 static, 그리고 `readonly`나 `"read"`로 쓰지 않겠다고 약속한 스토리지 바인딩에는 값을 쓸 수 없습니다. `const` 객체의 필드나 원소에 쓰는 것은 여기에 해당하지 않습니다. `const`는 객체를 담고 있을 뿐이고, 객체는 JavaScript에서처럼 계속 쓸 수 있기 때문입니다([규칙 6.10](https://typeshade.dev/ko/reference/rules/6-10/)).

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

임시 값처럼 쓸 수 있는 자리가 아닌 곳에 대입하거나 `++`/`--`를 씁니다. 모르는 이름은 `UNKNOWN_NAME`이고, 알려진 불변 바인딩에 쓰는 것은 `CONST_ASSIGN`입니다.

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

TypeShade가 풀지 못하는 이름을 가리킵니다. 함수 호출(`UNKNOWN_FN`)이나 타입 이름(`UNKNOWN_TYPE`)은 아닙니다. 라이브러리가 TypeScript 자체를 위해 선언한 이름(`Object`, `Math`, `Symbol`)이나 파일이 enum, 네임스페이스, 클래스, 타입으로 선언한 이름을 값으로 읽으면, 메시지가 그 이름이 무엇인지 알려 줍니다.

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

자기 코드를 받을 만하지 않은 진단을 위한 포괄 코드입니다. 순차 범위 뒤인 TS8099에 따로 둡니다.

## 함께 보기

- [규칙 8.16](https://typeshade.dev/ko/reference/rules/8-16/) 테스트
- [규칙 8.18](https://typeshade.dev/ko/reference/rules/8-18/) 테스트
- [규칙 6.10](https://typeshade.dev/ko/reference/rules/6-10/)
- [규칙 7.5](https://typeshade.dev/ko/reference/rules/7-5/)
- [규칙 8.8](https://typeshade.dev/ko/reference/rules/8-8/)
- [규칙 8.10](https://typeshade.dev/ko/reference/rules/8-10/)
- [규칙 8.13](https://typeshade.dev/ko/reference/rules/8-13/)
- [규칙 8.25](https://typeshade.dev/ko/reference/rules/8-25/)
- [설계 규칙](https://typeshade.dev/ko/reference/rules/)
- [오류 코드](https://typeshade.dev/ko/reference/errors/)

## 소스

커밋 af54ddad5의 규칙 원문:

- [`docs/language-design.md:984`](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/language-design.md#L984) (설계 문서)
- [`reqs/rules/RULE-0817.md`](https://github.com/typeshade/typeshade/blob/af54ddad5/reqs/rules/RULE-0817.md) (추적 항목)
