Stream: beginners

Topic: match multiple variants


view this post on Zulip removewingman (Sep 18 2026 at 21:39):

I want my row to have an is_eq function, is something like this possible/planned or is there anyway I can do this better?

Row := [
    Header(List(Cell)),
    Data(List(Cell)),
  ].{
    is_eq : Row, Row -> Bool
    is_eq = |left, right| match left, right {
        Header(a), Header(b) => a.is_eq(b)
        Data(a), Data(b) => a.is_eq(b)
        _ => False
    }
  }

view this post on Zulip Luke Boswell (Sep 18 2026 at 22:10):

This doesnt work? I feel like it should

view this post on Zulip removewingman (Sep 18 2026 at 22:21):

I get compile errors, if u want to give it a try:

Xdd := [].{
    Cell := {
        text : Str,
    }.{
        is_eq : Cell, Cell -> Bool
        is_eq = |left, right| left.text.is_eq(right.text)
        expect is_eq({ text: "a" }, { text: "a" }) == True
        expect is_eq({ text: "a" }, { text: "b" }) == False
    }

    Row := [
        Header(List(Cell)),
        Data(List(Cell)),
    ].{
        is_eq : Row, Row -> Bool
        is_eq = |left, right| match (left, right) {
            Header(a), Header(b) => a.is_eq(b)
            Data (a), Data(b) => a.is_eq(b)
            _ => False
        }
        expect Header([{ text: "a" }]).is_eq(Header([{ text: "a" }])) == True
        expect Header([{ text: "a" }]).is_eq(Header([{ text: "b" }])) == False
        expect Header([{ text: "a" }]).is_eq(Data([{ text: "a" }])) == False
        expect Data([{ text: "a" }]).is_eq(Data([{ text: "a" }])) == True
    }
}
I was parsing a match branch, and I expected `=>` before the branch body.

Header(a), Header(b) => a.is_eq(b)

view this post on Zulip Jared Ramirez (Sep 18 2026 at 22:25):

I think you need to wrap it in a tuple

view this post on Zulip Aurélien Geron (Sep 19 2026 at 02:20):

Can't you just use is_eq : _ ?

view this post on Zulip Luke Boswell (Sep 19 2026 at 03:09):

Cell := { text : Str }.{
    is_eq : _
}

Row := [Header(List(Cell)), Data(List(Cell))].{
    is_eq : _
}

main! = |_args| {
    header_a = Header([{ text: "a" }])

    header_b = Header([{ text: "b" }])

    data_a = Data([{ text: "a" }])

    dbg header_a.is_eq(header_a)
    dbg header_a.is_eq(header_b)
    dbg header_a.is_eq(data_a)
    dbg data_a.is_eq(data_a)

    Ok({})
}
$ roc example.roc
[dbg] True
[dbg] False
[dbg] False
[dbg] True

view this post on Zulip Richard Feldman (Sep 19 2026 at 03:28):

Jared Ramirez said:

I think you need to wrap it in a tuple

yeah, that's the issue - it's a syntax problem, not related to is_eq.

view this post on Zulip Richard Feldman (Sep 19 2026 at 03:29):

specifically, this:

            Header(a), Header(b) => a.is_eq(b)
            Data (a), Data(b) => a.is_eq(b)

...needs to be this:

            (Header(a), Header(b)) => a.is_eq(b)
            (Data (a), Data(b)) => a.is_eq(b)

view this post on Zulip removewingman (Sep 19 2026 at 06:35):

Aurélien Geron said:

Can't you just use is_eq : _ ?

Thank you, yes this is way easier, did not know that this was a possibility. (also to Luke)

I tried earlier wrapping it in a tuple, and this gives compile errors aswell (I want to understand the matching of multiple values and the Animal part works, what is going on):

Xdd := [].{
    Cell := {
        text : Str,
    }.{
        is_eq : Cell, Cell -> Bool
        is_eq = |left, right| left.text.is_eq(right.text)
        expect is_eq({ text: "a" }, { text: "a" }) == True
        expect is_eq({ text: "a" }, { text: "b" }) == False
    }

    Row := [
        Header(List(Cell)),
        Data(List(Cell)),
    ].{
        is_eq = |left, right| match (left, right) {
            (Header(a), Header(b)) => List.is_eq(a, b)
            (Data (a), Data(b)) => List.is_eq(a, b)
            _ => False
        }
        expect Header([{ text: "a" }]).is_eq(Header([{ text: "a" }])) == True
        expect Header([{ text: "a" }]).is_eq(Header([{ text: "b" }])) == False
        expect Header([{ text: "a" }]).is_eq(Data([{ text: "a" }])) == False
        expect Data([{ text: "a" }]).is_eq(Data([{ text: "a" }])) == True
    }

    Animal := [Dog(Str), Cat(Str)].{
        is_eq = |a, b| match (a, b) {
            (Dog(name1), Dog(name2)) => name1 == name2
            (Cat(name1), Cat(name2)) => name1 == name2
            _ => Bool.False
        }
    }
}
I was parsing a pattern, and this token cannot start a pattern here.

(Data (a), Data(b)) => List.is_eq(a, b)

Last updated: Sep 24 2026 at 15:59 UTC