matchExpr
이 페이지에서

matchExpr()

함수, 작성 분류

A typed multi-arm dispatch over a scalar scrutinee, the expression form of switch (scrutinee) { case v0: r0; …; default: dflt }.

import { matchExpr } from 'typeshade'

시그니처, 설명, 예제는 커밋 c66579bf의 컴파일러 소스에서 그대로 가져온 영어 원문입니다.

구문

function matchExpr<S extends ScalarKey, R extends string>(
scrutinee: ReadonlyNode<S>,
cases: ReadonlyArray<
readonly [caseValue: number, value: ReadonlyNode<R> | (() => ReadonlyNode<R>)]
>,
default_: ReadonlyNode<R> | (() => ReadonlyNode<R>),
): Node<R>

매개변수

scrutinee ReadonlyNode<S>
cases ReadonlyArray< readonly [caseValue: number, value: ReadonlyNode<R> | (() => ReadonlyNode<R>)] >
default_ ReadonlyNode<R> | (() => ReadonlyNode<R>)

반환값

Node<R>

예외

TypeShadeError

SD0011 when an arm’s type differs from the default’s.

설명

Each arm pairs an integer case value with its result, given either as a node or as a zero-argument function that builds one; the default takes the same two forms. Before emit, every matchExpr in a function body is rewritten into a var slot and a switch statement that writes each arm’s value into it. A match with ten or more arms casts a non-integer scrutinee to i32, since a WGSL switch is integer-only.

Every arm’s type must equal the default’s. The shared R extends string bound rejects most mismatches at tsc; one that reaches the runtime throws.

예제

예제

import { fn, matchExpr, f32, u32T } from 'typeshade'
const width = fn('width', { kind: u32T }, ({ kind }) =>
matchExpr(kind, [[0, f32(1)], [1, () => f32(2)]], f32(0.5)),
)

관련 가이드

함께 보기

소스

src/core/ir/node.ts, 2302행, 커밋 c66579bf 기준

이 페이지 편집 문제 보고