---
title: "규칙 8.18: 함수와 진입점, TypeShade 셰이더 언어 설계 규칙과 그 근거, 출처, 검증 방식"
description: "TypeShade 언어 설계 규칙 8.18(함수와 진입점)입니다. A function whose parameter has a function type, written out (f: (x…"
url: https://typeshade.dev/ko/reference/rules/8-18/
language: ko
---

# 규칙 8.18

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

A function whose parameter has a function type, written out (`f: (x: f32) => f32`) or through a type alias of one, takes a function: a function declared at the top of the file or of a namespace, a method, a static method, a constructor, a field that holds a function ([Rule 8.16](https://typeshade.dev/ko/reference/rules/8-16/)) and a local function ([Rule 8.17](https://typeshade.dev/ko/reference/rules/8-17/)). It is compiled once for each set of functions its calls hand it, as a generic function is once for each set of type arguments ([Rule 8.9](https://typeshade.dev/ko/reference/rules/8-9/)), and in each copy a call of the parameter calls the function handed over (`apply_sq`, `apply_run_f`, `Swarm_each_sq`, `run_twice_run_f`). A call hands a function over by its name (a module function, a local function, or a parameter of function type of the function the call is in) or as an arrow function or a function expression written in the call, which is a local function of the calling body (Rule 8.17) and takes its parameter types, and its return type where it writes none, from the parameter’s type. What the function handed over captures, the copy takes as parameters too and passes on, by reference where the function writes it. A variable the copy and a function handed over both reach is passed once, as TypeScript has it once: a variable a local function and the function it is handed both capture, and the object a method is called on when a function handed over captures that very variable (`this`, or a local named as the object), which the copy then takes as its object, by reference where the caller may write it ([Rule 8.10](https://typeshade.dev/ko/reference/rules/8-10/)). A function handed over takes the parameter’s types and returns its type, or any type where the parameter’s returns `void`, whose value is not used; an arrow function may leave parameters off at the end, as TypeScript allows, and one whose body is an expression and whose type returns `void` runs the expression as a statement. A parameter whose type returns an optional of `T` ([Rule 7.10](https://typeshade.dev/ko/reference/rules/7-10/), changes 0088 and 0098) also takes a function that returns `T`, or an optional of `T` with a subset of its absent states, as TypeScript assigns a function with a narrower result, and the copy made for that function lifts a `T` as present or widens an optional while preserving its absent tag, so a library may change a callback’s type to `T | undefined` and keep every callback that returns `T`. A parameter whose type declares an optional parameter, `f: (x?: f32) => f32` (Rule 7.10, change 0093, decision D5), passes `x` as `f32 | undefined`, and a call of the parameter that omits `x` passes the absent value, whatever the function handed over declares, `x: f32 | undefined` or `x?: f32`; a function declared `(x: f32) => f32` does not fit it, as TypeScript refuses that assignment under `strictNullChecks`, and is refused with the does-not-fit sentence ([`TS8003`](https://typeshade.dev/ko/reference/errors/ts8003/)); an arrow function written in the call may declare `x?: T` with the same meaning (decision D6), and one that writes `x?: f32` where the type passes a plain `f32` is refused (`TS8003`), which TypeScript accepts, a recorded divergence ([Rule 7.2](https://typeshade.dev/ko/reference/rules/7-2/)). Inside the function that takes it, a parameter of function type may only be called or handed on to another such parameter. A fold’s function (`any(xs, (x) => x > k)`, `zip(xs, ys, (a, b) => a * b)`) is handed over the same way, its types read off the arrays, and an arrow function whose return type nothing gives returns what its body does. An array’s method (`xs.map(f)`, `xs.forEach(f)`, `xs.some(p)`, `xs.every(p)`, `xs.reduce(f, init)`, surface §63) takes a function the same way, a function the file declares taking as few of the arguments as it declares: each call is a call of a function of the module made once for each array type and function handed over (`array_map_sq`), a counted loop over the indices ([Rule 7.5](https://typeshade.dev/ko/reference/rules/7-5/)) that hands the function the element, its index as an `i32` and the array, and for `reduce` the running value first, which has the type of the function’s first parameter where that is written and of the value to start from otherwise. The loop reads the array as it goes: a module variable, a module constant or a binding in place, the variable a function handed over captures through the one reference both use, and any other array by value, an index on the way to it read once. An array also has `fill(v)`, `findIndex(p)`, `findLastIndex(p)`, `indexOf(v)`, `lastIndexOf(v)` and `includes(v)` (change 0079), each with one argument and as ECMAScript runs it: an index is an `i32`, `-1` where nothing matches; `indexOf` and `lastIndexOf` compare with `===` and `includes` with SameValueZero, so a NaN is found by `includes` alone, told by its bits; the three take an `f32`, `i32`, `u32` or `bool` element, and `fill` writes a binding the shader may write, a kernel function’s array or a local array by its name, and gives back the array it fills, as ECMAScript does (change 0114, S2): `new Array(N).fill(v)` with a constant `N` is an `array<T, N>` of `N` copies of `v`, with `T` the type of `v`, and a use of what `xs.fill(v)` gives back on an array that already exists is [`TS8099`](https://typeshade.dev/ko/reference/errors/ts8099/), not supported yet. On a kernel function’s array parameter ([Rule 8.22](https://typeshade.dev/ko/reference/rules/8-22/), change 0079), `forEach`, `reduce`, `some`, `every`, `fill` and the searches are no call: the loop, and the function handed over, are written into the kernel function’s body at the statement the call is in, the loop counting with a `u32` and handing the function its index as an `i32`, `some`, `every` and `includes` as an `|` and an `&` over every element, and `findIndex`, `indexOf` and their `Last` forms as a `min` or a `max` of the index where it holds; a function handed over there ends in its one `return`, and a predicate does not write what it captures, since it runs for every element, or the call is refused. Of an array’s methods, one `Array.prototype` has beyond those eleven, a start or an end index, `map` on an array with no size, whose value would be an array with none, `reduce` with no value to start from on an array with no size, which may be empty, a second argument to any but `reduce` (`thisArg`), and a function a method passes an array with no size to must be refused. A function type anywhere else (a return, a field, a variable), a parameter of function type on a setter or an entry point, an argument that would choose a function at run time (`c ? f : g`), a function whose copies would never end (one that hands itself a function it builds anew each time), and a call on a field or an element of a variable that a function handed over reaches, where one of the two may write it, which would be two references into one variable, must be refused.

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

## 근거

TypeScript code hands functions to functions all the time: a callback to a loop helper, a predicate to a search, an operator to a fold. The GPU has no function value, but every call names the function it hands over, so each call reaches exactly one function, and a copy for each is what the program means; the copies are a generic function’s instances by another key, and the captures, the TDZ and the cycle check are [Rule 8.17](https://typeshade.dev/ko/reference/rules/8-17/)’s and [Rule 8.4](https://typeshade.dev/ko/reference/rules/8-4/)’s. Before this rule a function type was “Unsupported type syntax” and an arrow function written as an argument “Unsupported expression”. A copy of a method has an object, and a copy of a local function has captures of its own; each is one more parameter of the copy, and where one of them and a function handed over reach one variable, the copy takes one reference to it, since two references into one variable are refused by WGSL where either is written (Alias Analysis) and would let the method read a stale copy where they are not. A setter’s value is given by an assignment, which hands it no function. An array’s method is a loop, and a method call is an expression, which may stand in an argument, beside `&&` or in a loop’s condition, where no loop can be written in place; a function of the module is a call anywhere one may be, and every target and the CPU oracle run one already, so the IR gains no node. Unrolled, as the folds are, the code would grow with the array and would have no length to unroll to for a runtime-sized array, the one a compute kernel meets first. Before this rule every method of an array was refused.

## 출처

ECMAScript [function objects](https://tc39.es/ecma262/#sec-ecmascript-function-objects) (a function is a value a call hands over); ECMAScript [`Array.prototype.map`](https://tc39.es/ecma262/#sec-array.prototype.map) and its four siblings, which read each element as the loop reaches it; TypeScript’s contextual typing of a function expression and its rule that a function with fewer parameters is assignable; WGSL’s [Alias Analysis](https://gpuweb.github.io/gpuweb/wgsl/#alias-analysis); [Rule 8.4](https://typeshade.dev/ko/reference/rules/8-4/); [Rule 8.9](https://typeshade.dev/ko/reference/rules/8-9/); [Rule 8.10](https://typeshade.dev/ko/reference/rules/8-10/); [Rule 8.16](https://typeshade.dev/ko/reference/rules/8-16/); [Rule 8.17](https://typeshade.dev/ko/reference/rules/8-17/); surface §14, §26 and §63.

## 검증 방식

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

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

`TS8003 TYPE_MISMATCH` for a function that does not fit (`"add" takes 2 argument(s), and "(x: f32) => f32" passes 1, so it cannot be "f" of "apply".`; for a parameter that takes `T` where the type passes `T | undefined`, `"plain" takes f32 for argument 1, where "(x?: f32) => f32" passes f32 | undefined, so it cannot be "f" of "apply".`, from `misfit` in `src/compiler/ts/lower/function-types.ts`, change 0093), for an arrow function’s parameter written `x?: f32` where the type passes `f32` (`"x" is written f32 | undefined, and "(x: f32) => f32" passes f32.`, `src/compiler/ts/lower/function-args.ts`, change 0093), [`TS8099`](https://typeshade.dev/ko/reference/errors/ts8099/) for a choice at run time (`"f" of "apply" takes a function, which a call hands over by its name or as an arrow function written there; "c ? sq : cube" would choose one at run time, and a shader has no function value to choose with.`), `TS8020 FUNCTION_SHAPE` for a parameter of function type where none may be (`"f" takes a function, and "G.each" is a setter, whose value an assignment gives it: a shader has no function value to assign. Take the function in a method instead (Rule 8.18).`), [`TS8002`](https://typeshade.dev/ko/reference/errors/ts8002/) for a function type anywhere else, `TS8031 RECURSION` for copies without end, and `TS8099` for two references into one variable (`"f" of "Inner.each" reaches "this", which holds "this.inner", the object the call is on, so the call would take two references into one variable, which WGSL refuses where either is written. Call it on a copy in a let, and assign the copy back if the call changes it (Rule 8.18).`); for an array’s methods, `TS8099` for another method (`".filter" is not one of an array's methods here, which are map, forEach, some, every, reduce, fill, findIndex, findLastIndex, indexOf, lastIndexOf and includes. …`), for `map` on an array with no size and for a function handed that array, and `TS8019 ARITY_MISMATCH` for `reduce` with no value to start from on one and for a `thisArg`; `wrapHandedResult` in `src/compiler/ts/lower/expression-misc.ts` for the result of a function that returns `T` handed where `T | undefined` is taken, pinned by `src/compiler/ts/optional-values.test.ts` and `examples/gallery/optional-values/optional-values.shade.ts` (`sampleOr_ramp`, change 0088); pinned by `src/compiler/ts/higher-order.test.ts` and `src/compiler/ts/array-methods.test.ts`, which hold WGSL, GLSL ES 3.00, the CPU oracle, the codegen and the debugger to one value for each form, on a fixed-size array and on a runtime-sized storage one; `src/compiler/ts/array-constructors.test.ts` holds both halves, the WGSL and the CPU oracle to `new Array(N).fill(v)`, `new Float32Array(N)`, `Array.from` and the value `fill` gives back (change 0114, S2); `examples/gallery/higher-order/higher-order.shade.ts` and `examples/gallery/array-methods/array-methods.shade.ts` in the compile gate; the light-list journey on WebGPU.

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

- [`examples/shade-examples.test.ts:27`](https://github.com/typeshade/typeshade/blob/af54ddad5/examples/shade-examples.test.ts#L27)
- [`src/compiler/ts/array-constructors.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/array-constructors.test.ts#L1)
- [`src/compiler/ts/array-methods.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/array-methods.test.ts#L1)
- [`src/compiler/ts/higher-order.test.ts:50`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/higher-order.test.ts#L50)
- [`src/compiler/ts/lower/expression-misc.ts:111`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/lower/expression-misc.ts#L111)
- [`src/compiler/ts/lower/function-args.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/lower/function-args.ts#L1)
- [`src/compiler/ts/lower/function-types.ts:75`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/lower/function-types.ts#L75)
- [`src/compiler/ts/optional-values.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/optional-values.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)
- [§26 Classes with methods, a constructor and static functions](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/use-typeshade-surface.md#26-classes-with-methods-a-constructor-and-static-functions)
- [§27 Boolean vectors](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/use-typeshade-surface.md#27-boolean-vectors)
- [§63 An array's methods](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/use-typeshade-surface.md#63-an-arrays-methods)
- [§76 Optional values: `T | undefined`, `T | null` and three states](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/use-typeshade-surface.md#76-optional-values-t--undefined-t--null-and-three-states)

## 적용하는 오류 코드

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

**[`TS8002`](https://typeshade.dev/ko/reference/errors/ts8002/) `UNKNOWN_TYPE`**

아무것도 선언하지 않은 타입 이름입니다. 매개변수, 타입 인자, 별칭, `implements` 절, 기반 클래스 어디에 써도 걸리고, 이 표면이 매퍼가 읽는 자리에서 받지 않는 타입도 여기에 속합니다([규칙 2.1](https://typeshade.dev/ko/reference/rules/2-1/)). 파일이 선언했지만 구조체로 모으지 않은 기반은 `STRUCT_FIELD`입니다.

**[`TS8003`](https://typeshade.dev/ko/reference/errors/ts8003/) `TYPE_MISMATCH`**

연산자나 조건, 선언, 반환, 인자처럼 두 값이 만나는 자리에서 타입이 서로 맞지 않거나, 검사로 좁히지 않은 선택적 값(`T | undefined`, `T | null`, `T | null | undefined`)을 읽거나([규칙 7.10](https://typeshade.dev/ko/reference/rules/7-10/)), 컴파일러가 접는 상수를 WGSL이 거부합니다. 31을 넘는 시프트 양, 0인 제수, 서로 엇갈린 clamp 경계, 범위를 넘는 `f32` 값, `Math.pow(-2., 2.)`처럼 정의역 밖에서 부르는 내장 함수가 그렇습니다.

**[`TS8019`](https://typeshade.dev/ko/reference/errors/ts8019/) `ARITY_MISMATCH`**

호출이나 생성자에서 인자, 요소, 필드의 개수가 맞지 않습니다.

**[`TS8020`](https://typeshade.dev/ko/reference/errors/ts8020/) `FUNCTION_SHAPE`**

TypeShade가 지원하지 않는 함수 선언이나 매개변수 모양입니다. 이름이나 본문이 없는 경우, 나머지 매개변수, 구조 분해 매개변수가 그렇습니다.

**[`TS8031`](https://typeshade.dev/ko/reference/errors/ts8031/) `RECURSION`**

호출 순환입니다. 함수가 직접, 또는 다른 함수를 거쳐 자기 자신에 닿습니다. WGSL에는 호출 스택이 없으므로 컴파일러가 타깃이 보기 전에 순환을 거부합니다.

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

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

## 함께 보기

- [규칙 8.17](https://typeshade.dev/ko/reference/rules/8-17/) 테스트
- [규칙 8.19](https://typeshade.dev/ko/reference/rules/8-19/) 테스트
- [규칙 7.2](https://typeshade.dev/ko/reference/rules/7-2/)
- [규칙 7.5](https://typeshade.dev/ko/reference/rules/7-5/)
- [규칙 7.10](https://typeshade.dev/ko/reference/rules/7-10/)
- [규칙 8.4](https://typeshade.dev/ko/reference/rules/8-4/)
- [규칙 8.9](https://typeshade.dev/ko/reference/rules/8-9/)
- [규칙 8.10](https://typeshade.dev/ko/reference/rules/8-10/)
- [규칙 8.16](https://typeshade.dev/ko/reference/rules/8-16/)
- [규칙 8.22](https://typeshade.dev/ko/reference/rules/8-22/)
- [설계 규칙](https://typeshade.dev/ko/reference/rules/)
- [오류 코드](https://typeshade.dev/ko/reference/errors/)

## 소스

커밋 af54ddad5의 규칙 원문:

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