gleam icon indicating copy to clipboard operation
gleam copied to clipboard

`use` in combination with pipelines produces a confusing type error

Open joshi-monster opened this issue 7 months ago • 4 comments

Hi! 💜

I noticed this after adding a |> result.try at the end of a long pipeline and use - ing the result. I've made a simplified example demonstrating the problem:

fn use_test(input: Int, callback: fn(result: String) -> result) -> result {
   callback(int.to_string(input))
}

fn f(input: Int) -> String {
  use result: String <- input |> use_test
  "The result is: " <> result
}

produces the following type error:

error: Type mismatch
   ┌─ /src/main.gleam:15:34
   │
15 │   use result: String <- input |> use_test
   │                                  ^^^^^^^^

Expected type:

    fn(Int, fn(String) -> a) -> a

Found type:

    fn(Int) -> b

Playing with the types for a bit, it seems like the "Expected type" here is always just the type of the function at the end of the pipeline, and the "Found type" is the type of the function with the second argument (the callback) partially applied.

When the function expects more than 2 arguments, you get an "Incorrect arity" error instead, even when providing the remaining "middle" arguments to the function (Expected N arguments, got N-2)

joshi-monster avatar Jun 27 '24 18:06 joshi-monster