Stream: bugs

Topic: Effectful function error not good


view this post on Zulip Brendan Hansknecht (Dec 28 2024 at 19:27):

This error is pretty bad. Very hard to realize what is wrong. Took me a while before I noticed the difference:

The argument is an anonymous function of type:

    { … }, Str => { … }

But walk needs its 3rd argument to be:

    { … }, Str -> { … }

view this post on Zulip Brendan Hansknecht (Dec 28 2024 at 19:27):

We should make this message clearer somehow

view this post on Zulip Brendan Hansknecht (Dec 28 2024 at 19:27):

Explicitly state the function is effectful but the caller requires the function to be pure.

view this post on Zulip Sam Mohr (Dec 28 2024 at 19:52):

Sounds like we need two changes

view this post on Zulip Sam Mohr (Dec 28 2024 at 19:53):

@Brendan Hansknecht do you have the full code? I'm happy to reduce to a repro

view this post on Zulip Brendan Hansknecht (Dec 28 2024 at 19:54):

roc check on this:

app [main!] { pf: platform "../platform/main.roc" }

import pf.Stdout
import pf.File

dir_list : List Str

main! = \_args ->
    foo = List.walk dir_list { file: [], dir: [] } \state, elem ->
        when File.is_file! elem is
            Ok is_file if is_file ->
                { state & file: List.append state.file elem }

            Ok _ ->
                { state & dir: List.append state.dir elem }

            _ ->
                # Just ignore failures
                state

    Stdout.line! "Hello, World!"

view this post on Zulip Brendan Hansknecht (Dec 28 2024 at 19:55):

will see:

This 3rd argument to walk has an unexpected type:

 9│>      foo = List.walk dir_list { file: [], dir: [] } \state, elem ->
10│>          when File.is_file! elem is
11│>              Ok is_file if is_file ->
12│>                  { state & file: List.append state.file elem }
13│>
14│>              Ok _ ->
15│>                  { state & dir: List.append state.dir elem }
16│>
17│>              _ ->
18│>                  # Just ignore failures
19│>                  state

The argument is an anonymous function of type:

    { … }, Str => { … }

But walk needs its 3rd argument to be:

    { … }, Str -> { … }

view this post on Zulip Sam Mohr (Dec 28 2024 at 20:19):

Thanks!

view this post on Zulip Sam Mohr (Dec 28 2024 at 20:57):

Pretty simple:

module [broken, effectful!]

effectful! : Str => Result Str []

broken = List.map [] effectful!

Produces

── TYPE MISMATCH in test.roc ───────────────────────────────────────────────────

This 2nd argument to map has an unexpected type:

5│  broken = List.map [] effectful!
                         ^^^^^^^^^^

This effectful! value is a:

    Str => Result Str []

But map needs its 2nd argument to be:

    Str -> Result Str []

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

1 error and 0 warnings found in 30 ms.

view this post on Zulip Sam Mohr (Dec 28 2024 at 21:10):

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


Last updated: Jul 05 2025 at 12:14 UTC