3 ms·
The `use` syntax is interesting - don't recall seeing anything similar before. But I'm struggling to understand how exactly it is executed and a glance at the G
by rossng 2y ago
The `use` syntax is interesting - don't recall seeing anything similar before. But I'm struggling to understand how exactly it is executed and a glance at the Gleam docs didn't help.
Is the `use` statement blocking (in which case it doesn't seem that useful)? Or does it return immediately and then await at the point of use of the value it binds?
- jyjasdfsssd 2y agoIt is syntax sugar for CPS [1]. [1]: https://en.wikipedia.org/wiki/Continuation-passing_style https://en.wikipedia.org/wiki/Continuation-passing_style EDIT: I believe prior art is Koka's with statement: https://koka-lang.github.io/koka/doc/book.html#sec-with https://koka-lang.github.io/koka/doc/book.html#sec-with
- rossng 2y agoHmm, it definitely looks more interesting in combination with effect handlers. Still not sure I find it super compelling in Gleam vs just not using callbacks.
- jitl 2y agoIt’s a generalization of async/await syntax in languages like JavaScript or Swift. I like that it provides a generalized syntax that could be used for coroutines, generators, or async/await without adding any of those specifically to the language syntactically. One level of callback nesting in a function is totally fine, two is a bit confusing, but if you have many async things going on do you really want 10, 15, 20 levels of nesting? What to do about loops? I certainly greatly prefer async programming with async/await languages that keep the appearance of linear function execution to stacking my callbacks and having a ton of nesting everywhere
- vips7L 2y agoSounds like the new “capabilities” stuff in Scala.
- yawaramin 2y agoYou can do something similar in OCaml (as an operator defined at the library level, not a specialized new syntax): https://github.com/yawaramin/letops/blob/6954adb65f1156597405149d5d0116599b1d2049/letops.mli#L18 https://github.com/yawaramin/letops/blob/6954adb65f115659740...
- taberiand 2y agoThe equivalent in F# is let! (F# computation expressions are quite powerful); in rust the ? operator. Other languages have similar features. It's syntactic sugar, but the readability is worth it
- eddd-ddde 2y agoI think it's similar to koka's 'with'. https://koka-lang.github.io/koka/doc/book.html#sec-with https://koka-lang.github.io/koka/doc/book.html#sec-with
- skybrian 2y agoEverything after the line containing '<-' happens in a callback. Since it's a callback, I assume it's up to the function whether to call it, when to call it, and how many times to call it, so this can implement control statements. I would guess that it also allows it to be async (when the callback isn't called until after an I/O operation).
- cprecioso 2y agoIt really reminds me of LiveScript's "back-calls" [1], which were a solution for callback hell in JS. 1: https://livescript.net/#:~:text=Backcalls%20are%20very%20useful https://livescript.net/#:~:text=Backcalls%20are%20very%20use...
- klibertp 2y agoThat was way more than a solution for callback hell. With some plumbing, you could get Continuation monad working! With no further support from the language, too. I really miss LiveScript, it's a shame its development stopped. If only it could emit TypeScript, it would still have a chance to fight back, I think.
- giacomocava 2y agoThere's a great article by Erika on use, definitely recommended :) https://erikarow.land/notes/using-use-gleam https://erikarow.land/notes/using-use-gleam