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, _)
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.
Oskar Hahn has marked this topic as resolved.
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.
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:
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)?
Bryce Miller has marked this topic as unresolved.
Does anyone have any ideas for ways to make this API nicer for 0.1?
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?
(Thunks aside, it's looking like I should have roc-pg ready to go soon!)
hey Bryce! :smiley:
I have a WIP solution for this case
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
and I assume these cases wouldn't need where clauses?
The only where clauses I have are on methods within Pg.Client, nothing in the encoding/decoding paths.
Also Hi, I have reappeared :sweat_smile:
Bryce Miller has marked this topic as resolved.
Last updated: Nov 26 2026 at 20:35 UTC