News for hackers. Stuff that matters.
Re:Zig: Upcoming Release Postponed Two More Weeks and Lacks ... by ksec on Wednesday July 19, 2023 @10:52AM (#36787417)
Why Async, especially considering Zig intends to be something like a High Level "portable Assembly"?
Re:Zig: Upcoming Release Postponed Two More Weeks and Lacks ... by anonymoushn on Wednesday July 19, 2023 @11:41AM (#36788227)
Zig's async, like Lua's, lets people write libraries that are generic over async-ness by default. "dump my registers and jump to another stack" is also a legitimate move in assembly (though that's now how this implementation actually works).
Re:Zig: Upcoming Release Postponed Two More Weeks and Lacks ... by formerly_proven on Wednesday July 19, 2023 @12:03PM (#36788619)
I understand the technical reasons (caused by cultural reasons) why Python ended up with colored async but man oh man was it a bad choice for a high-overhead language to do that…
Re:Zig: Upcoming Release Postponed Two More Weeks and Lacks ... by DaiPlusPlus on Wednesday July 19, 2023 @02:24PM (#36790968)
(you’re referring to this article [1], right?)

What alternative approach would you have preferred Python do? While I’m not a fan of the coloured-function approach either, I can’t think of a better alternative that checks-off the most boxes (Java’s Loom looks interesting but in-a-way it seems too-good-to-be-true to me, while low-cost approaches like Zig’s would introduce too much complexity for novice users and people wanting something that just-works)

[1]: https://journal.stuffwithstuff.com/2015/02/01/what-color-is-...

Re:Zig: Upcoming Release Postponed Two More Weeks and Lacks ... by gpderetta on Wednesday July 19, 2023 @03:04PM (#36791570)
gevent?
Re:Zig: Upcoming Release Postponed Two More Weeks and Lacks ... by laserbeam on Wednesday July 19, 2023 @10:59AM (#36787554)
Waiting for IO is never cool. Also, zig's version of async is pretty low level and still obeys the no hidden control flow mantra (at least how it was implemented until 0.9.0).
Re:Zig: Upcoming Release Postponed Two More Weeks and Lacks ... by chubot on Wednesday July 19, 2023 @12:10PM (#36788733)
How was it implemented in the past? Is there any overview of the design iterations Zig has gone through?

Seems like Rust is still working on this area with the controversial keyword generics

Re:Zig: Upcoming Release Postponed Two More Weeks and Lacks ... by somethingor on Wednesday July 19, 2023 @12:27PM (#36789020)
Re:Zig: Upcoming Release Postponed Two More Weeks and Lacks ... by hinkley on Wednesday July 19, 2023 @01:41PM (#36790275)
I can't tell from the examples. Does Zig's async support closures or is it strictly out of order execution?
Re:Zig: Upcoming Release Postponed Two More Weeks and Lacks ... by wredue on Wednesday July 19, 2023 @01:52PM (#36790453)
Zig does not have closures, and probably never will barring someone coming up with a nice, no hidden flow idea for captures.
Re:Zig: Upcoming Release Postponed Two More Weeks and Lacks ... by laserbeam on Wednesday July 19, 2023 @02:00PM (#36790574)
It's similar to manual control over the current stack. You pause execution, you dump your current stack onto the heap, and restore it later. Control over concurrency, similar to out of order execution. I'm describing it conceptually but I assume the implementation can be/was smarter/faster.

I don't think closures have anything to do with it.

There was also some debate about how cancelation of an awaitable function built with those primitives would work. I don't think there was a great answer for that yet.

Re:Zig: Upcoming Release Postponed Two More Weeks and Lacks ... by Dwedit on Wednesday July 19, 2023 @01:29PM (#36790086)
How the heck would that even work...

In C#, they implement Await/Async by converting the function into a Class, just like when you use 'yield return'. Control flow is all over the place.

Zig is so strict about "no hidden control flow" that you can't even have destructors (code which runs when a variable goes out of scope)

Re:Zig: Upcoming Release Postponed Two More Weeks and Lacks ... by wredue on Wednesday July 19, 2023 @01:44PM (#36790311)
Huh? Lots of language do not have guaranteed to be called at a specific time “destructors”.

Additionally, zig provides this functionality with defer and errdefer, giving you the ability to explicitly call a function at the end of scope.

Re:Zig: Upcoming Release Postponed Two More Weeks and Lacks ... by DaiPlusPlus on Wednesday July 19, 2023 @02:14PM (#36790775)
> Lots of language do not have guaranteed to be called at a specific time “destructors”.

Those tend to be GC languages though, where deterministic execution is not a hard requirement.

Re:Zig: Upcoming Release Postponed Two More Weeks and Lacks ... by wredue on Wednesday July 19, 2023 @03:17PM (#36791753)
Sure, but this is also what defer is for.

There isn’t implicitly called destructors because the languages mantra is “no hidden control flow”. RAII et semantics is almost entirely hidden control flow that you just have to “know”.

Lots of people hate when they need to “just know” things to fully parse the code they’re reading.

You’re free to dislike those decisions, of course. Personally, I like the target of no hidden flow.

Re:Zig: Upcoming Release Postponed Two More Weeks and Lacks ... by bsder on Wednesday July 19, 2023 @04:00PM (#36792337)
I have a love/hate with this.

The big problem with defer/errdefer is that I have no way to mark a function as "This function always needs a defer after it and the compiler needs to yell at me if it doesn't exist."

It's also sometimes really hard to scope your defer/errdefer correctly. You may have to twist your code inside out because defer/errdefer ends at a block scope while your variable may not (via: "break :blk varname;").

This all bites so hard for things like reference counting. The bugs ... the bugs ... <mumbles huddled up in corner>

Re:Zig: Upcoming Release Postponed Two More Weeks and Lacks ... by kprotty on Thursday July 20, 2023 @01:15PM (#36803585)
You can `errdefer/defer if` inside the blk, then x = blk:{}; defer use(x)` outside the block.
Re:Zig: Upcoming Release Postponed Two More Weeks and Lacks ... by bsder on Thursday July 20, 2023 @04:52PM (#36806590)
That wasn't a request for help (I have debugged my reference counts already), but I thank you nonetheless.

You can do as you say, but that winds up being a lot of non-enforced boilerplate. And you can get subtle errors if you miss the defer at the wrong scope (ask me how I know this ... actually, better yet, please don't as it will give me flashbacks :) ). And neither the language nor compiler can help you. This contrasts with, say, Rust where reference counts or locks can be enforced by the compiler and simply never go wrong.

defer/errdefer is obvously vastly better than C. My codebase was painful in C. Zig made it tractable, but it was hardly pleasant.

Unfortunately, I really don't have a good suggestion as to what Zig should do instead. defer/errdefer is a minimum, but it's not clear what a single, better step further actually would be. Most solutions in other languages wind up with RAII and that invokes a nightmare of cascading design decisions through a programming language that Zig very much does not want to follow.

It will be interesting to see where async/await finally lands. I think that will have some component of a "slightly better" solution.

Re:Zig: Upcoming Release Postponed Two More Weeks and Lacks ... by pjmlp on Thursday July 20, 2023 @02:53PM (#36805011)
Depends on the language, and the set of automatic memory management tooling available.
Re:Zig: Upcoming Release Postponed Two More Weeks and Lacks ... by laserbeam on Wednesday July 19, 2023 @02:04PM (#36790628)
Yup. The lack of destructors means that at the time there wasn't an agreed upon way to cancel an awaitable function. Async/await in zig was cool, but there's a reason why it's described as an experimental feature in the article.
Re:Zig: Upcoming Release Postponed Two More Weeks and Lacks ... by kprotty on Wednesday July 19, 2023 @04:19PM (#36792599)
Destructors don't seem to be the best place to implement cancellation for async functions: Rust async is going through a phase where the community is realizing it would be nice to have async destructors or non-cancellable async functions to avoid introducing unnecessary overhead like concurrent reclamation, reference counting, and dynamically allocated memory for things that would otherwise be done statically using structured concurrency.
Re:Zig: Upcoming Release Postponed Two More Weeks and Lacks ... by Bigpet on Wednesday July 19, 2023 @11:32AM (#36788079)
Having some form of coroutines or green threads seems like a good idea to me for something low-level. Otherwise countless projects that need something like it will re-implent their own incompable version with longjmp or similar means.
Re:Zig: Upcoming Release Postponed Two More Weeks and Lacks ... by himujjal on Wednesday July 19, 2023 @11:11AM (#36787771)
Go through Zig’s Async. Its a bit like co-routines but low level. You are required to keep track of memory allocations and pointers to function “frames”. For someone coming from a high-level language POV, frames were a new concept. But Zig does it so superb.

Zig just needs some runtime event loop like Tokio or AsyncIO from Rust to get up and running with its fantastic async model.

Re:Zig: Upcoming Release Postponed Two More Weeks and Lacks ... by kissgyorgy on Wednesday July 19, 2023 @11:58AM (#36788534)
Zig's async implementation is the best of any languages, because you can run them without an event loop and in that case they will be simply synchronous. This completely eliminates the function coloring problem.
Re:Zig: Upcoming Release Postponed Two More Weeks and Lacks ... by dgb23 on Wednesday July 19, 2023 @12:07PM (#36788681)
Does that mean the return types are the same when you use async or not on the same function?
Re:Zig: Upcoming Release Postponed Two More Weeks and Lacks ... by pornel on Wednesday July 19, 2023 @12:15PM (#36788826)
No, there still are sync and async versions of functions. Zig just implicitly chooses which version to run and implicitly inserts await points. There's still a runtime and event loop if you use async. It's an async like in any language, but it relies on compiler magic instead of having locally explicit syntax.
Re:Zig: Upcoming Release Postponed Two More Weeks and Lacks ... by matu3ba on Wednesday July 19, 2023 @12:28PM (#36789054)
One can check this via static analysis as the complete code tree can be inferred with relative ease (only package paths and usingnamespace are depending on comptime) + third party code will be able to query compiler info, so I do not expect this to be a big problem once static analysis symbols can be enforced as unique (see https://github.com/ziglang/zig/issues/14656#issuecomment-143... and follow-up for the use cases of static analysis tooling). Also, since there is currently no problem: YAGNI and solve it once a use case comes up.
Re:Zig: Upcoming Release Postponed Two More Weeks and Lacks ... by kprotty on Wednesday July 19, 2023 @12:43PM (#36789348)
No, async functions still return an async Frame (the semantic equivalent of a Rust Future) which evaluates to the result, rather than the result itself. Zig's "colorless async" description comes from the implicit infectiousness by default rather than colors not "being there":

A function which contains a suspend point becomes implicitly async. An await is implicitly a suspend until the Frame completes, so it also makes the caller's function async. Calling a async function without the `async` keyword is implicitly `await (async f(args))` so same deal.

You ended up not needing to know if `f()` is async or not when you call it ("effectively colorless"). If it is, it just makes you async and bubbles up until the sync boundary (an `async f()` call, `main()`, or an `export fn` C API).

Zig's stdlib could handle the main() case and spin up an event loop if you requested via `io_mode = .evented`. Some stdlib stuff like net/fs would then use the event loop while others like os/Thread would stay the same if you still wanted to do sync stuff.

This was the common case, but async is a lang construct not an stdlib construct so it works for all targets. For example, you could use it in wasm, the kernel, etc. You would just need to write your own starting / scheduling of the frames, known generically as an "event loop".

Re:Zig: Upcoming Release Postponed Two More Weeks and Lacks ... by xxpor on Wednesday July 19, 2023 @12:03PM (#36788608)
Usually the coloring problem IME goes the other way: you want to run a synchronous/blocking function in an async context. How does Zig deal with that?
Re:Zig: Upcoming Release Postponed Two More Weeks and Lacks ... by lvass on Wednesday July 19, 2023 @12:11PM (#36788756)
Function calls?
Re:Zig: Upcoming Release Postponed Two More Weeks and Lacks ... by Nullabillity on Wednesday July 19, 2023 @07:18PM (#36794881)
And now you're blocking the event loop.
Re:Zig: Upcoming Release Postponed Two More Weeks and Lacks ... by lvass on Wednesday July 19, 2023 @07:49PM (#36795150)
If you chose to use cooperative concurrency, it's because you can make it cooperate. Use preemption otherwise.
Re:Zig: Upcoming Release Postponed Two More Weeks and Lacks ... by j16sdiz on Wednesday July 19, 2023 @01:38PM (#36790230)
They have await keyword. This is noop in sync mode
Re:Zig: Upcoming Release Postponed Two More Weeks and Lacks ... by kprotty on Thursday July 20, 2023 @09:30AM (#36800152)
Did you mean the `nosuspend` keyword? It only asserts nothing suspends rather than there being a "sync" mode.
Re:Zig: Upcoming Release Postponed Two More Weeks and Lacks ... by continuational on Wednesday July 19, 2023 @04:41PM (#36792989)
* * *
Re:Zig: Upcoming Release Postponed Two More Weeks and Lacks ... by kprotty on Thursday July 20, 2023 @09:28AM (#36800123)
Zig async doesn't change semantics of how things are called. If something is called sequentially, it will execute sequentially with respect to itself, with or without async.
Re:Zig: Upcoming Release Postponed Two More Weeks and Lacks ... by kissgyorgy on Thursday July 20, 2023 @10:52AM (#36801355)
It doesn't change the semantics, the documentation says: https://ziglang.org/documentation/0.10.1/#Async-Functions

"Async functions can be called the same as normal functions." Only when you have an event loop, the order async functions are running might be different because of the scheduling, but everything else is the same.

Re:Zig: Upcoming Release Postponed Two More Weeks and Lacks ... by pjmlp on Thursday July 20, 2023 @02:55PM (#36805062)
Except Modula-2 and Concurrent Pascal, among several others did it first, in the 1970's.
Re:Zig: Upcoming Release Postponed Two More Weeks and Lacks ... by pjmlp on Thursday July 20, 2023 @02:51PM (#36804990)
Modula-2, Mesa, Concurrent Pascal, NEWP all had coroutines support, so actually that is something I like in Zig.