Stream: beginners

Topic: ✔ scope of bindings inside when


view this post on Zulip Michał Łępicki (Oct 17 2022 at 06:54):

Why is this an error?

    newCount =
        when previous is
            { previous1: Some previous1, previous2: Some previous2, previous3: Some previous3 } ->
                if previous2 + previous3 + measurement > previous1 + previous2 + previous3 then
                    count + 1
                else
                    count

            _ -> count
    { previous2, previous3 } = previous

I'm getting

── DUPLICATE NAME ─────────────────────────────────────────────────── 1/1b.roc ─

The previous2 name is first defined here:

37│      { previous2, previous3 } = previous
         ^^^^^^^^^^^^^^^^^^^^^^^^

But then it's defined a second time here:

30│              { previous1: Some previous1, previous2: Some previous2, previous3: Some previous3 } ->
                                                              ^^^^^^^^^

I find it odd that the name bindings introduced inside when patterns clash with things introduced later outside of when. Also, note that it says "second time" pointing to an earlier line :thinking:

view this post on Zulip Brian Carroll (Oct 17 2022 at 07:13):

Yeah it looks like "earlier" and "later" must be based on the order they appear in the compiler's syntax tree, rather than in the source program text. We should probably change the way it's phrased.
For context: the order of definition does not technically matter at all in Roc. All local definitions are just subexpressions of the returned value.
So this was a wrong assumption that was made when choosing the wording of the error message.

view this post on Zulip Brian Carroll (Oct 17 2022 at 07:14):

If you'd like to open an issue to improve this error message, it would be welcome!

view this post on Zulip Michał Łępicki (Oct 17 2022 at 07:18):

#4339

view this post on Zulip Notification Bot (Oct 17 2022 at 07:18):

Michał Łępicki has marked this topic as resolved.


Last updated: Jul 06 2025 at 12:14 UTC