---
title: "Let(): function in Authoring, TypeShade API reference"
description: "Let, a function in Authoring. Bind a value to an immutable local, emitting let name = expr; into the innermost active scope."
url: https://typeshade.dev/api/let/
language: en
---

# `Let()`

Function in Authoring

Bind a value to an immutable local, emitting let name = expr; into the innermost active scope.

`import { Let } from 'typeshade/core/ir'`

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

## Syntax

```ts
function Let<K extends string>(value: ReadonlyNode<K>): ReadonlyNode<K>;
function Let<K extends string>(name: string, value: ReadonlyNode<K>): ReadonlyNode<K>;
```

## Parameters

**`value` `ReadonlyNode<K>`**

the expression to bind.

**`name`**

the emitted binding name. Omit it and the builder generates one.

## Return value

`ReadonlyNode<K>`

a read-only node reading the binding.

## Description

Bind a value to an immutable local, emitting `let name = expr;` into the innermost active scope. It is the ambient counterpart of `Builder.let`, resolving the innermost active builder itself so a body reads plainly without threading `b` through every nested [`If`](https://typeshade.dev/api/if/) or [`Loop`](https://typeshade.dev/api/loop/) callback.

It returns the read-only node type, so `binding.assign(...)` is a tsc error. To mutate, declare with [`Var`](https://typeshade.dev/api/var/). Read APIs take the read-only type too, so a `Let` result flows everywhere a value is read.

Most intermediates need no wrapper at all: author a plain `const` and the emit decides between inlining, a shared `let` and a `var`. The case where a `Let` is load-bearing is a loop that mutates a var. Common-subexpression elimination cannot cache a subexpression that reads a mutated var, because its value differs per read site, so a value derived from that var re-emits at every read unless it is materialised. Inside such a loop, wrap anything derived from the mutated var that you read more than once.

## Examples

### Example

```ts
import { fn, Loop, Let, If, Break, length, f32, u32, f32T, vec3fT } from 'typeshade/core/ir'

const march = fn('march', { ro: vec3fT, rd: vec3fT }, f32T, ({ ro, rd }) => {
  const t = f32(0) // mutated below, so the emit materialises it as a var
  Loop(u32(0), (i) => i.lt(u32(72)), () => {
    const p = ro.add(rd.mul(t))
    const d = Let(length(p).sub(1)) // computed once; without Let it re-emits per read
    If(d.lt(0.001), () => Break())
    t.assign(t.add(d))
  })
  return t
})
```

## See also

- [`If`](https://typeshade.dev/api/if/)
- [`Loop`](https://typeshade.dev/api/loop/)
- [`Var`](https://typeshade.dev/api/var/)

## Source

[`src/core/ir/builder.ts`, line 1338](https://github.com/typeshade/typeshade/blob/af54ddad5/src/core/ir/builder.ts#L1338), at commit af54ddad5
