#What does the `use` Syntactic Sugar expand to in this Case?

1 messages · Page 1 of 1 (latest)

willow beacon
#

I was reading the "Use sugar" section of the Gleam Language Tour, wherein a simplified example is provided of use

use a, b <- my_function
next(a)
next(b)

which expands to

my_function(fn(a, b) {
  next(a)
  next(b)
})

From my observation, it appears that the expansion is done, as follows:

  1. a and b are arguments to the anonymous function
  2. The body of the anonymous function has the lines next(a) and next(b)
  3. Finally, my_function is called with the anonymous function as a callback.

On the code section of the screen, there's another example of use

let x = {
    use username <- result.try(get_username())
    use password <- result.try(get_password())
    use greeting <- result.map(log_in(username, password))
    greeting <> ", " <> username
  }

However, I'm unclear how this is expanded. I'm not sure how I can apply the steps I observed in the first example to the second case.

drifting crypt
#

that expands to

let x = result.try(get_username(), fn(username) {
  result.try(get_password(), fn(password) {
    result.map(log_in(username, password), fn(greeting) {
      greeting <> ", " <> username
    })
  })
})
willow beacon
#

Can the anonymous function be made as the first argument, like:

let x = result.try(fn(username) {
  ...
}, get_username())
drifting crypt
#

thats not how use works, no

#

use is a syntax transformation specifically for callbacks, eg when the last argument is a function