News for hackers. Stuff that matters.
Re:Gleam Is Pragmatic by rossng on Sunday October 06, 2024 @03:04PM (#41759321)
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?

Re:Gleam Is Pragmatic by taberiand on Sunday October 06, 2024 @04:19PM (#41759960)
The 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

Re:Gleam Is Pragmatic by yawaramin on Sunday October 06, 2024 @04:01PM (#41759832)
You 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/6954adb65f115659740...
Re:Gleam Is Pragmatic by jyjasdfsssd on Sunday October 06, 2024 @03:13PM (#41759412)
It is syntax sugar for CPS [1].

[1]: 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

Re:Gleam Is Pragmatic by rossng on Sunday October 06, 2024 @03:34PM (#41759602)
Hmm, 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.
Re:Gleam Is Pragmatic by jitl on Sunday October 06, 2024 @04:18PM (#41759946)
It’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

Re:Gleam Is Pragmatic by vips7L on Sunday October 06, 2024 @05:46PM (#41760601)
Sounds like the new “capabilities” stuff in Scala.
Re:Gleam Is Pragmatic by eddd-ddde on Sunday October 06, 2024 @05:22PM (#41760427)
I think it's similar to koka's 'with'.

https://koka-lang.github.io/koka/doc/book.html#sec-with

Re:Gleam Is Pragmatic by skybrian on Sunday October 06, 2024 @10:13PM (#41762157)
Everything 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).

Re:Gleam Is Pragmatic by cprecioso on Monday October 07, 2024 @04:36AM (#41763935)
It 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%20use...

Re:Gleam Is Pragmatic by klibertp on Monday October 07, 2024 @08:36AM (#41765385)
That 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.
Re:Gleam Is Pragmatic by giacomocava on Monday October 07, 2024 @06:14AM (#41764496)
There's a great article by Erika on use, definitely recommended :) https://erikarow.land/notes/using-use-gleam