rescript-compiler icon indicating copy to clipboard operation
rescript-compiler copied to clipboard

[Feature Request] Gleam-like `use` expressions?

Open sudo97 opened this issue 1 year ago • 8 comments
trafficstars

Thank you for filing! Check list:

  • [ ] Is it a bug? Usage questions should often be asked in the forum instead.
  • [ ] Concise, focused, friendly issue title & description.
  • [ ] A minimal, reproducible example.
  • [ ] OS and browser versions, if relevant.
  • [ ] Is it already fixed in master? Instructions

I found it so neat and flexible, I am surprised no one else has done this before. Gleam Use Expressions

I think gleam has very similar semantic to ReScript and this is a very useful sugar

sudo97 avatar Feb 05 '24 13:02 sudo97

Yep, we don't have a way to bind the JavaScript's Disposable/AsyncDisposable and to use using keyword yet, which has recently reached stage 3 and TypeScript support.

cometkim avatar Feb 06 '24 13:02 cometkim

I don't think it's really the same thing, it can be one case of it though... It's more like OCaml's let* and let+ just generalized

sudo97 avatar Feb 06 '24 14:02 sudo97

I'm very fresh to compiler implementation, but I'd perhaps give it a try when I'm ready

sudo97 avatar Feb 06 '24 14:02 sudo97

AFAIK monadic let binding is one of the features we gave up in choosing a representation closer to JS. Support for some generalized syntax that's not common in JS is still controversial, but support for those widely present in the JS standard is clearly something that is needed.

cometkim avatar Feb 06 '24 14:02 cometkim

I don't think it's up to me to dictate language design, it's just what I feel is easy enough to grasp, and can allow lots of possibilities. It's just a sugar for callback passing.

let x = {
   use a <- f;
   f2(a);
}

Would be de-sugared into

let x = f(a => f2(a));

So when async/await solve one case of callback-hell, very valuable, ngl. But I feel like use could cover async/await if needed, and do a lot more. Even more than monadic bind, actually.

So I'm not going to push for it, that is my opinion, so if it doesn't fit the vision, please disregard this suggestion, we can close it

sudo97 avatar Feb 06 '24 14:02 sudo97

As I can see, these two code examples are not the same.

DZakh avatar Feb 07 '24 09:02 DZakh

That is indeed a correct observation, the former is with suggested syntactic sugar, the latter is without. Perhaps I messed up the semicolon? Apologies, I was typing through my phone. Since ReScript does not provide that syntax yet, I'll post a gleam snippet here for demonstration.

import gleam/io
import gleam/bool

pub fn main() {
  let x = {
    use a <- f
    f2(a)
  }
  
  let y = f(fn(a) {f2(a)})
  
  io.print(bool.to_string(x == y))
}

fn f2(x) {
  x + 1
}

fn f(cb) {
   cb(5)
}

It can be pasted in here. The idea is to perhaps add this syntactic sugar into ReScript. For deeper dive I really recommend reading the link I initially posted, it can work as async/await, as an Option.flatMap, as Option.map too, it just flattens the callback hell of any kind for you

sudo97 avatar Feb 08 '24 11:02 sudo97

Yep, we don't have a way to bind the JavaScript's Disposable/AsyncDisposable and to use using keyword yet, which has recently reached stage 3 and TypeScript support.

I don't think this feature request is related to Disposables / using. And its also not equivalent to let* It's really just an alternative to passing callbacks.

https://tour.gleam.run/advanced-features/use-sugar/

doeixd avatar Feb 27 '24 20:02 doeixd