If
On this page

If()

Function in Authoring

Author if (cond) { body } over the innermost active scope.

import { If } from 'typeshade'

The signature, the description and the examples come from the compiler's own source at commit 26de7be8.

Syntax

const If: (
cond: ReadonlyNode<"bool">,
body: () => ReadonlyNode | void,
) => IfChain

Parameters

cond ReadonlyNode<'bool'>

the branch condition.

body () => ReadonlyNode | void

the statements of the branch, authored through the ambient functions.

Return value

IfChain

the chain, for .elif and .else.

Exceptions

SD0115

SD0115 when the body returns a value.

Description

Chain .elif(c, () => …) and .else(() => …) on the returned IfChain. A native return inside the body callback only exits that closure; for an early return from the branch write Return or ReturnIf.

Because that value goes nowhere, a body that returns one is rejected with SD0115 instead of being accepted as an empty branch: If(c, () => f32(1)) differs from If(c, () => Return(f32(1))) by three characters and by everything else. For a value, reach for when.

Examples

Example

If(d.lt(0.001), () => {
Break()
}).else(() => {
t.assign(t.add(d))
})

In the guide

See also

Source

src/core/ir/builder.ts, line 1805, at commit 26de7be8

Edit this page Report a problem