Stream: beginners

Topic: ✔ Is there a better syntax for this match de-construction ?


view this post on Zulip Fábio Beirão (Jun 09 2023 at 09:39):

I find myself doing this pattern more often than not. Is there a better syntax/patten I could use for this?
On one hand I want to deconstruct the pattern so I can access properties of the payload (for instance, for Dir I use .children, but on the other hand, I still need the element with the constructor+payload (Dir or File).

I "wish" there was something like (pseudo-code)

when diskObject is
    (Dir { children } as dirInner) as dirOuter -> ...
    (File fileInner) as fileOuter -> ...

This is what I do at the moment, but feels clunky:
image.png

view this post on Zulip Anton (Jun 09 2023 at 10:03):

You can use diskObject again instead of the suggested as xOuter.

view this post on Zulip Fábio Beirão (Jun 09 2023 at 10:03):

Let me try that, didn't occur to me

view this post on Zulip Anton (Jun 09 2023 at 10:05):

Yeah, it took me a while as well :)

view this post on Zulip Fábio Beirão (Jun 09 2023 at 10:06):

It works! You're a boss @Anton :pray:

image.png

view this post on Zulip Notification Bot (Jun 09 2023 at 10:06):

Fábio Beirão has marked this topic as resolved.

view this post on Zulip Brendan Hansknecht (Jun 09 2023 at 14:14):

Extra note, i think the as syntax your wrote above should work in pattern matching. I just don't think it does currently.


Last updated: Jul 06 2025 at 12:14 UTC