Stream: beginners

Topic: matching while extracting from record


view this post on Zulip Brendan Hansknecht (Dec 12 2022 at 19:21):

Should something like this work?

SomeType : [
    A { name: Str, other: U64 },
    B { name: Str, somethingElse: List U32 },
]

var : SomeType
var = A { name: "test", other: 32 }

main =
    when var is
        A { name } | B { name } ->
            Stdout.line name

view this post on Zulip Brendan Hansknecht (Dec 12 2022 at 19:21):

In current roc, the two instances of name in the match will not unify because the underlying record type has different fields.

view this post on Zulip Brendan Hansknecht (Dec 12 2022 at 19:24):

Oh, actually even if the records are the same, it still doesn't work in current roc. So really any record in general blocks | from being used.

view this post on Zulip Kevin Gillette (Dec 13 2022 at 03:57):

I would expect, as a beginner, that any disjoints within a when case to be exactly equivalent to if those disjoints were each given their own case (with the case body essentially just being duplicated in terms of semantic effect).


Last updated: Jul 06 2025 at 12:14 UTC