Hi there. I was experimenting with var's and tried using them in the context of destructuring a tuple.
However, the compiler is not happy about it so I'm wondering if this use case is even supported or not.
Code:
main! = |_| {
Ok({})
}
g = |index| {
(0, index)
}
f = |i| {
(_, var $index) = g(i)
(_, $index) = g($index)
$index
}
Compiler output:
➜ roc git:(main) ✗ roc check main.roc
-- DUPLICATE DEFINITION --------------------------
The name $index is being redeclared in this scope.
The redeclaration is here:
┌─ .../roc/main.roc:11:6
│
11 │ (_, $index) = g($index)
│ ^^^^^^
But $index was already defined here:
┌─ .../roc/main.roc:10:6
│
10 │ (_, var $index) = g(i)
│ ^^^^^^^^^^
-- INVALID ASSIGNMENT TO ITSELF ------------------
The value $index is assigned to itself, which would cause an infinite loop at runtime.
Only functions can reference themselves (for recursion). For non-function values, the right-hand side must be fully computable without referring to the value being assigned.
┌─ .../roc/main.roc:11:18
│
11 │ (_, $index) = g($index)
│ ^^^^^^
Found 1 error(s) and 1 warning(s) in 5693ms for main.roc.
I think it's not yet supported, it looks a bit weird but it feels inconsistent not to allow it so I am leaning towards supporting it.
Actually if you declare the var on its own line first you’re able to at least reassign it from a tuple destructure (last I checked with nightly from a week ago), I thought it was a cool pattern for threading a $rand_seed around. I guess in your case you’d be forced to assign var $index some throwaway value at the beginning though? So maybe it does make sense to allow it to be declared in the first destructure with (_, var $index)
EDIT:
nevermind I guess your example could start with var $index = i
double edit:
Sorry if the whole point of the post was about being able to declare a var in the middle of a destructure and my solution is kind of tangential lol
iirc you can foward declare a var without a value like:
var $index
(_, $index) = g(i)
...
(note you'll get an error if you try to use it before assigning to it)
Last updated: Jul 23 2026 at 13:15 UTC