Stream: beginners

Topic: Type inferencing generic type in same module


view this post on Zulip Ian McLerran (Jun 16 2024 at 02:29):

So I came across a strange type inferencing issue today:

If I define a function which uses type variables, and then I write an expect statement which applies that function to multiple types, I get errors in the earlier applications of the function, complaining that they do not match the later applications.

However if I split my expects into a separate Test.roc module, I have no problem applying the function using multiple types.

So for example:

reduceListRight : List a, (a, a -> a) -> Result a [ListWasEmpty]

expect reduceListRight [] Num.add == Err ListWasEmpty
expect reduceListRight [5] Num.add == Ok 5
expect reduceListRight ["1", "2", "3"] Str.concat == Ok "321"

Produces the following errors:

── TYPE MISMATCH in Reduce.roc ─────────────────────────────────────────────────
This 1st argument to reduceListRight has an unexpected type:
24│  expect reduceListRight [5] Num.add == Ok 5
                            ^^^
The argument is a list of type:
    List (Num *)

But reduceListRight needs its 1st argument to be:
    List Str

── TYPE MISMATCH in Reduce.roc ─────────────────────────────────────────────────
This 2nd argument to reduceListRight has an unexpected type:
24│  expect reduceListRight [5] Num.add == Ok 5
                                ^^^^^^^
This add value is a:
    Num a, Num a -> Num a

But reduceListRight needs its 2nd argument to be:
    Str, Str -> Str

── TYPE MISMATCH in Reduce.roc ─────────────────────────────────────────────────
This 2nd argument to == has an unexpected type:
24│  expect reduceListRight [5] Num.add == Ok 5
                                           ^^^^
This Ok tag application has the type:
    [Ok (Num *), …]

But == needs its 2nd argument to be:
    [Ok Str, …]

view this post on Zulip Luke Boswell (Jun 16 2024 at 03:37):

I think your missing part of the module here. The listReduceRight is not the same as reduceListRight.

view this post on Zulip Ian McLerran (Jun 16 2024 at 03:52):

Oh... haha! Partial copy/pasta, partial re-typing my code lol.

view this post on Zulip Ian McLerran (Jun 16 2024 at 03:56):

(I fixed the mis-type)

view this post on Zulip Brendan Hansknecht (Jun 16 2024 at 05:10):

And this is specific to expect? Doesn't happen with regular use?

view this post on Zulip Ian McLerran (Jun 16 2024 at 05:39):

Thats a great question. I'm haven't actually tried calling the function from within the module except inside expect. Its a module in a package, not a application, but I can see if it passes roc check.

view this post on Zulip Ian McLerran (Jun 16 2024 at 05:40):

Or maybe better yet, write a function which calls reduceListRight with a couple different types, and do an expect on that function...


Last updated: Jul 05 2025 at 12:14 UTC