This is all good:
cons = \h, t -> Cons h t
empty = Empty
consList = \l -> l |> List.walkBackwards empty \state, elem -> cons elem state
consList [1,2,3]
Cons 1 (Cons 2 (Cons 3 Empty)) : [Cons (Num *) a, Empty] as a
but I get weird errors if I add type annotations
ConsList a : [Cons a (ConsList a), Empty]
cons : a, ConsList a -> ConsList a
cons = \h, t -> Cons h t
empty : ConsList a
empty = Empty
consList : List a -> ConsList a
consList = \l -> l |> List.walkBackwards empty \state, elem -> cons elem state
gives odd errors:
── TYPE MISMATCH in test.roc ───────────────────────────────────────────────────
This 3rd argument to |> has an unexpected type:
12│ consList = \l -> l |> List.walkBackwards empty \state, elem -> cons elem state
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The argument is an anonymous function of type:
ConsList a, a -> ConsList a
But |> needs its 3rd argument to be:
ConsList a, a -> ConsList a
── TYPE MISMATCH in test.roc ───────────────────────────────────────────────────
Something is off with the body of the consList definition:
11│ consList : List a -> ConsList a
12│ consList = \l -> l |> List.walkBackwards empty \state, elem -> cons elem state
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This walkBackwards call produces:
ConsList a
But the type annotation on consList says it should be:
ConsList a
────────────────────────────────────────────────────────────────────────────────
Yeah, that looks like a bug, can you file an issue?
Sure thing :+1:
https://github.com/roc-lang/roc/issues/6879
Lachlan O'Dea has marked this topic as resolved.
Last updated: Jul 06 2025 at 12:14 UTC