Should using an as
pattern inside a local destructuring be possible?
sample =
(Test a as b) = Test {}
{}
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
What do a and b mean here, and what is the longform version of your intended assignment?
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.
your intension is to do not write it in 2 lines ?
b = Test 42
(Test a) = b
yeah, not surprising, past lists, I don't think as is used regularly
So probably has bugs
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
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
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
Oh, yeah, and as @JanCVanB pointed out, if there are multiple variants to those tags, you need to use a when ... is
Act 2: naming unknown pieces
to clarify, Test
is just a wrapper tag, not from a union
lue has marked this topic as resolved.
Last updated: Jul 06 2025 at 12:14 UTC