
On this ninth day of Nockmas, we will explore opcode 8, Extend and how Nock implements variable binding.
Opcode 8: Extend
Syntax
*[a 8 b c] *[[*[a b] a] c]
Explanation
Opcode 8 pins a new value to the head of the subject, then evaluate the body. This is how Nock implements variable binding—the new value becomes accessible at address 2.
- Evaluate
bagainst subject to produce a new value. - Construct
[new-value subject]as the extended subject. - Evaluate
cagainst the extended subject.
After opcode 8, addresses shift:
- Address 2 → the new pinned value
- Address 3 → the original subject
- Address 6 → what was address 2
- Address 7 → what was address 3
Opcode 8 extends the subject, while opcode 7 replaces it:
*[a 7 b c] → *[*[a b] c] :: New subject is *[a b] *[a 8 b c] → *[[*[a b] a] c] :: New subject is [*[a b] a]
There are two primary patterns for using opcode 8. In the first, a local variable is bound for use in a computation:
*[data [8 [compute-value] [body-using-value-at-+2]]]
We start with our subject:
:subject 42
Output:
Subject set to: 42
And call a Nock 8 with the following formula:
[8 [1 41] [[0 2] [0 3]]]
Output:
[41 42]
The other, more complex, pattern is to pin a core and its arms to the subject for invocation later. We'll see this pattern in detail in opcode 9's discussion.
*[context [8 [1 battery] [9 2 [0 1]]]]
Join us tomorrow when we cover Nock 9, Invoke.
12 Days of Nockmas is an exploration of Nock, Urbit's instruction set architecture. This ISA is used by both Urbit and Nockchain, has interpreters written in many languages, with production versions in both C and Rust. The content of this series is drawn from the Nock language site. Visit the site for interactive code examples and more Nock related content.