Stream: compiler development

Topic: var from tuple destructuring


view this post on Zulip JRI98 (Jun 06 2026 at 14:18):

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.

view this post on Zulip Anton (Jun 06 2026 at 14:39):

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.


Last updated: Jun 16 2026 at 16:19 UTC