Stream: beginners

Topic: ✔ Why does adding type annotations fail here?


view this post on Zulip Lachlan O'Dea (Jul 06 2024 at 11:31):

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

────────────────────────────────────────────────────────────────────────────────

view this post on Zulip Anton (Jul 06 2024 at 12:03):

Yeah, that looks like a bug, can you file an issue?

view this post on Zulip Lachlan O'Dea (Jul 06 2024 at 12:04):

Sure thing :+1:

view this post on Zulip Lachlan O'Dea (Jul 06 2024 at 12:26):

https://github.com/roc-lang/roc/issues/6879

view this post on Zulip Notification Bot (Jul 06 2024 at 12:26):

Lachlan O'Dea has marked this topic as resolved.


Last updated: Jul 06 2025 at 12:14 UTC