Stream: beginners

Topic: ✔ The `map_try` function expects 2 arguments, but only g...


view this post on Zulip Ian Liu Rodrigues (Jan 31 2025 at 17:53):

I have this simple program that I am trying to parse the command line arguments from a basic-cli app, but I cannot please the roc alpha2 compiler. It is complaining that map_try expects 2 arguments, and I'm passing only 1. But the map_try is in a pipeline, I was expecting this to work. Any help?

app [main!] { pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.19.0/Hj-J_zxz7V9YurCSTFcFdu6cQJie4guzsPMUi5kBYUk.tar.br" }

import pf.Arg
import pf.Stdout

parse_arg = |arg|
    { after, before } = Str.split_first(arg, ".")?
    Ok (Str.to_dec(before)?, Str.to_dec(after)?)

main! : List Arg.Arg => Result {} [Exit I32 Str]
main! = |args|
    args
    |> List.map(Arg.display)
    |> List.map_try(parse_arg) ? |_| Exit 1 "Invalid arg"
    |> List.for_each! |(day, sub)| Stdout.line! "Solving problem forday ${Num.to_str day} sub ${Num.to_str sub}" ?? {}
    |> Ok

The error message:

The map_try function expects 2 arguments, but it got only 1:

14│      |> List.map_try(parse_arg)
            ^^^^^^^^^^^^

And version:

$ roc --version
roc nightly pre-release, built from commit 689c58f on Mi 29 Jan 2025 09:02:05 UTC

view this post on Zulip Sam Mohr (Jan 31 2025 at 18:04):

When I change main! to

main! : List Arg.Arg => Result {} [Exit I32 Str]
main! = |args|
    parsed_args : Result (List (Dec, Dec)) _
    parsed_args =
        args
        |> List.map(Arg.display)
        |> List.map_try(parse_arg)

    Ok({})

It typechecks

view this post on Zulip Sam Mohr (Jan 31 2025 at 18:05):

So I think it's probably a weird interaction between whitespace function calls and parens function calls?

view this post on Zulip Sam Mohr (Jan 31 2025 at 18:05):

The new ? binop operator may be confused around this

view this post on Zulip Anton (Jan 31 2025 at 18:14):

@Ian Liu Rodrigues for now, you can replace

|> List.map_try(parse_arg) ? |_| Exit 1 "Invalid arg"

with

|> List.map_try(parse_arg)
|> Result.map_err(|_| Exit(1, "Invalid arg"))?

view this post on Zulip Ian Liu Rodrigues (Jan 31 2025 at 18:20):

Sweet, thanks!

view this post on Zulip Notification Bot (Jan 31 2025 at 18:20):

Ian Liu Rodrigues has marked this topic as resolved.

view this post on Zulip Anton (Jan 31 2025 at 18:21):

Should the original code work @Sam Mohr? If so, I'll make an issue for it

view this post on Zulip Sam Mohr (Jan 31 2025 at 18:21):

It should yes

view this post on Zulip Sam Mohr (Jan 31 2025 at 18:22):

Not super worried, depending on how long WSA stays around

view this post on Zulip Sam Mohr (Jan 31 2025 at 18:22):

We should make an issue regardless

view this post on Zulip Anton (Jan 31 2025 at 18:25):

#7567


Last updated: Jul 06 2025 at 12:14 UTC