Stream: beginners

Topic: ✔ as pattern in local destructuring


view this post on Zulip lue (Dec 10 2024 at 18:06):

Should using an as pattern inside a local destructuring be possible?

sample =
    (Test a as b) = Test {}

    {}

view this post on Zulip Sam Mohr (Dec 10 2024 at 18:13):

I'd suggest we take the as out of the parens, but yes, this should definitely be a thing that works if it doesn't already

view this post on Zulip jan kili (Dec 10 2024 at 18:17):

What do a and b mean here, and what is the longform version of your intended assignment?

view this post on Zulip lue (Dec 10 2024 at 18:18):

a and b are pattern variables here. Extra parens for clarity:

sample =
    ((Test a) as b) = (Test {})

    {}

right now, putting an as anywhere in a (nested) pattern used on the left of a local destructuring doesn't compile.

view this post on Zulip Ghislain (Dec 10 2024 at 18:20):

your intension is to do not write it in 2 lines ?

b = Test 42
(Test a) = b

view this post on Zulip Brendan Hansknecht (Dec 10 2024 at 18:20):

yeah, not surprising, past lists, I don't think as is used regularly

view this post on Zulip Brendan Hansknecht (Dec 10 2024 at 18:20):

So probably has bugs

view this post on Zulip lue (Dec 10 2024 at 18:22):

the example is just something minimal. splitting into multiple can become a bit more involved if you have more nested as.

sample =
    ((Test a) as first, (....) as second, (....) as third) = triple

    ....

I don't even want to advocate for as, just wondering if there was a reason.
For maybe extra context, I'm transpiling elm (which allows as everywhere) to roc which is why I noticed this in the first place.

Do yall think I should open an issue for this? Edit: https://github.com/roc-lang/roc/issues/7328

view this post on Zulip jan kili (Dec 10 2024 at 18:26):

In that longer example, I see no need for as because it's safer to do those two acts in two steps
Act 1: naming known pieces
Act 2: naming unknown pieces

Act 1 (safe):

triple : (Foo, Bar, Baz)

(first, second, third) = triple

Act 2 (unsafe):

Test a = first

Act 2 (safe):

when first is
    Test a -> ...
    Toast a -> ...
    _ -> ...

Edit: nvm, you're assuming type safety, which is reasonable

view this post on Zulip Brendan Hansknecht (Dec 10 2024 at 18:26):

Yeah, probably just never used. So bugs. Please file with the minimal repro. I assume a workaround would be:

(first, second, third) = triple
Test a = first
.... = second
.... = third

view this post on Zulip Brendan Hansknecht (Dec 10 2024 at 18:27):

Oh, yeah, and as @JanCVanB pointed out, if there are multiple variants to those tags, you need to use a when ... is

view this post on Zulip lue (Dec 10 2024 at 18:27):

Act 2: naming unknown pieces

to clarify, Test is just a wrapper tag, not from a union

view this post on Zulip Notification Bot (Dec 13 2024 at 16:12):

lue has marked this topic as resolved.


Last updated: Jul 06 2025 at 12:14 UTC