Stream: beginners

Topic: ✔ Open Tag unions on parsers


view this post on Zulip Anton (Nov 21 2026 at 18:24):

Only function definitions can generalize. Non-function values have exactly one type and annotations can only make the type stricter, not more flexible.

You can however use _ like this:

parse_left : Parser(List(U8), _, _)
parse_left = Parser.const(|n| Left(n)).skip(Parser.Utf8.codeunit('L')).keep(Parser.Utf8.digits({}))

parse_right : Parser(List(U8), _, _)
parse_right = Parser.const(|n| Right(n)).skip(Parser.Utf8.codeunit('R')).keep(Parser.Utf8.digits({}))

Or, define the union once and use it everywhere:

Either : [Left(U64), Right(U64)]

parse_line : Parser(List(U8), Either, _)
parse_left : Parser(List(U8), Either, _)
parse_right : Parser(List(U8), Either, _)

view this post on Zulip Oskar Hahn (Nov 22 2026 at 08:08):

Anton said:

Only function definitions can generalize. Non-function values have exactly one type and annotations can only make the type stricter, not more flexible.

Ok. I hope this will change in the future. I am not entirely sold on the workarounds. I often skip the type annotations and only add them, if there is a bug with the inferred type and I want to isolate it to one function. So the placeholder _does not help in this situation. Its similar with the "global type". It does not help to understand, what a simple function does.

I will try the different workarounds and see, what will work best.

view this post on Zulip Notification Bot (Nov 22 2026 at 08:09):

Oskar Hahn has marked this topic as resolved.

view this post on Zulip Anton (Nov 22 2026 at 08:11):

Ok. I hope this will change in the future

I suspect that it's like this for a good reason, I will do some research.

view this post on Zulip Richard Feldman (Nov 22 2026 at 16:14):

yeah it'll be like this for 0.1.0 - I have some ideas for things to try, but it's a tricky problem because of what constants compile to in Roc, so the restriction means we don't have to solve it right now :sweat_smile:

view this post on Zulip Bryce Miller (Nov 23 2026 at 02:25):

Incidentally I have run into this as well while working on updating my fork of roc-pg. Basically, my solution has just been to add thunks everywhere. I was hoping the thunks wouldn't make it to the public API but alas:

    rows = Pg.Cmd.new("a query")
        .bind([Pg.Cmd.str("John"), Pg.Cmd.u8(32)])
        .expect_n(
            {
                name: Result.str({})("name"),
                age: Result.u8({})("age"),
            }.Result,
        )
        .send!(client)?

view this post on Zulip Notification Bot (Nov 23 2026 at 02:27):

Bryce Miller has marked this topic as unresolved.

view this post on Zulip Bryce Miller (Nov 23 2026 at 02:27):

Does anyone have any ideas for ways to make this API nicer for 0.1?

view this post on Zulip Bryce Miller (Nov 23 2026 at 02:27):

e.g., given that Roc has a feature freeze right now, is there a different way I can design the query parsing API that doesn't require thunks?

view this post on Zulip Bryce Miller (Nov 23 2026 at 02:54):

(Thunks aside, it's looking like I should have roc-pg ready to go soon!)

view this post on Zulip Richard Feldman (Nov 23 2026 at 03:02):

hey Bryce! :smiley:

view this post on Zulip Richard Feldman (Nov 23 2026 at 03:02):

I have a WIP solution for this case

view this post on Zulip Richard Feldman (Nov 23 2026 at 03:03):

it's a fairly invasive change, but it turns out that (unless the implementation runs into something unexpected), we can change the restriction from "no type variables allowed in constants" to just "no where clauses allowed in constants" without any major downside

view this post on Zulip Richard Feldman (Nov 23 2026 at 03:03):

and I assume these cases wouldn't need where clauses?

view this post on Zulip Bryce Miller (Nov 23 2026 at 03:24):

The only where clauses I have are on methods within Pg.Client, nothing in the encoding/decoding paths.

view this post on Zulip Bryce Miller (Nov 23 2026 at 03:28):

Also Hi, I have reappeared :sweat_smile:

view this post on Zulip Notification Bot (Nov 23 2026 at 03:47):

Bryce Miller has marked this topic as resolved.


Last updated: Nov 26 2026 at 20:35 UTC