Var()
함수, 작성 분류
Declare a mutable local and return the node that reads and writes it.
import { Var } from 'typeshade'
시그니처, 설명, 예제는 커밋 c66579bf의 컴파일러 소스에서 그대로 가져온 영어 원문입니다.
구문
function Var<K extends string>(init: ReadonlyNode<K>): Node<K>;function Var<T extends ShaderType>(type: T, init?: ReadonlyNode<KeyOf<T>>): Node<KeyOf<T>>;function Var<T extends ShaderType>( name: string, type: T, init?: ReadonlyNode<KeyOf<T>>,): Node<KeyOf<T>>;function Var<K extends string>(name: string, init: ReadonlyNode<K>): Node<K>;매개변수
initReadonlyNode<K>the initial value.
namethe emitted identifier. Omit it and the builder generates one.
typethe declared type. Omit it when an initialiser carries the type.
반환값
Node<K>
a mutable node reading the binding, and the target of .assign.
설명
Var is the one
declarator whose result carries .assign, so it is what a body reaches for when a value
changes: an accumulator, a counter, a value a Switch chain fills in per case.
Four shapes are accepted: Var(init) infers the type from the initialiser, Var(type) and
Var(type, init) state it, and Var(name, init) or Var(name, type, init) also pin the
emitted identifier, which keeps the bytes stable across rebuilds.
A plain const usually needs no Var. Author the intermediate as a JavaScript const and
the emit decides between an inlined expression, a shared let and a var; if the value is
later assigned to, the auto-var pass materialises it as a real var with no marker from
you. Reach for Var when you want the binding named, or when the declaration and the first
assignment are far apart.
The read-only bindings are the other half of the rule. A Let result, an fn
parameter and a module constant all return the read-only node type, which has no .assign,
so mutating one is a tsc error at the authoring line.
예제
예제
import { Var, Switch, f32 } from 'typeshade'
const radiusPx = Var('radius_px', rawRadius)Switch(sizeMode) .case(1, () => radiusPx.assign(rawRadius.div(viewport.z))) .default(() => {})관련 가이드
함께 보기
소스
src/core/ir/builder.ts, 1658행, 커밋 c66579bf 기준