Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Gleam-like use expressions? #6612

Open
5 tasks
sudo97 opened this issue Feb 5, 2024 · 8 comments
Open
5 tasks

[Feature Request] Gleam-like use expressions? #6612

sudo97 opened this issue Feb 5, 2024 · 8 comments

Comments

@sudo97
Copy link

sudo97 commented Feb 5, 2024

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

@cometkim
Copy link
Member

cometkim commented Feb 6, 2024

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.

@sudo97
Copy link
Author

sudo97 commented Feb 6, 2024

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
Copy link
Author

sudo97 commented Feb 6, 2024

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

@cometkim
Copy link
Member

cometkim commented Feb 6, 2024

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.

@sudo97
Copy link
Author

sudo97 commented Feb 6, 2024

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

@DZakh
Copy link
Contributor

DZakh commented Feb 7, 2024

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

@sudo97
Copy link
Author

sudo97 commented Feb 8, 2024

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

@doeixd
Copy link

doeixd commented Feb 27, 2024

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/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants