#'use' + pipe operator

1 messages · Page 1 of 1 (latest)

spice stream
#

Hi, I'm learning Gleam by doing the exercises on https://exercism.org and I encountered this little issue:

fn foo(condition: Bool) -> String {
  use <- condition |> bool.guard("OK")
  "KO"
}

It says: Incorrect arity. Expected 3 arguments, got 2.

(This sample is a simplification of the real code)

white sonnet
#

That's right, you've not supplied all the arguments to bool.guard there

#

use doesn't have a special case where it behaves differently for pipe, so what you're doing doesn't work

spice stream
#

I thought it would be the same as use <- bool.guard(condition, "OK")

steep nexus
#

From the compiler POV that's extra work to figure out

#

bool.guard(condition, "OK") is a call expr
condition |> bool.guard("OK") is a pipeline expr

white sonnet
#

use always does either:

  1. Inserts the function as the final argument if it's a call
  2. Turns it into a call if it isn't and then does 1.

There is no special case for pipe and there never will be.

steep nexus
#

they're similar but not the same

spice stream
#

ok, I understand, thank you for your answers

pine trail
#

In general I'd avoid doing that because it's difficult to read, even if it worked

white sonnet
#

That's our motivation for not adding a special case there

spice stream
daring stratus
#

Gleam doesn't have function currying

#

That's not what the pipe operator is

white sonnet
#

There's no technical limitation, it's a language design matter