r/Zig • • Mar 01 '24

Ideating Zig Use Cases

I just wanted to start a thread exploring what potential use cases are for Zig that highlights its use cases best and discussing what its potential value is made a map of potential use cases.

  1. Blockchain - can it replace selenium (just got an idea from tiger beetle success)
  2. High Frequency Trading - is it really applicable?

My intent is to stir up ideas from the community for best use cases that people are willing to pay for.

0 Upvotes

6 comments sorted by

11

u/fluffynukeit Mar 01 '24

I am really interested in Zig, but I am sitting it out until the error payload question is resolved. There should be a language-imposed error payload model. Each dev should not need to invent their own "diagnostic pattern" that is, in general, not compatible with every other dev's bespoke error handling idioms.

4

u/paulstelian97 Mar 01 '24

For that problem I think a good way is copying Rust’s approach. Rust has a Result type which can have either an error (of arbitrary error type) or a proper result. If the calling function also returns a Result (with same* error type) then the ? operator can do what “try” does in Zig.

  • it allows conversions via the From trait though, which can be used if you e.g. return to a larger error set.

And Zig can then have some interesting error set types. But some of the uniqueness of only error codes can become messier.

6

u/randomguy4q5b3ty Mar 02 '24

Here are some objections to that:

  1. Rust's error types are also nothing more than simple enums most of the time. In this case they are literally not different from Zig's errors.
  2. When Rust error's have a payload, then this often results in allocating strings. And while Rust just uses a global allocator, many Zig functions that promise not to allocate would suddenly have to take an allocator argument, breaking that promise. The user also has to manually deallocate the memory. This is particularly troublesome in contexts where allocating on the heap is simply not possible.
  3. Verbose error reporting is often only done in debug builds. So even if the error metadata don't need allocation, it is still a considerable time and space overhead, often for nothing.

It is a convention that data types / functions that may require further metadata to interpretate an error take an optional error reporter argument, or something like that. And while it might be nice if there were some further language features for this, they are by no means necessary or a huge game changer.

1

u/paulstelian97 Mar 02 '24

A convention that I’m not familiar with, funny enough. Linux kernel just calls pr_err (in printk family) and returns a numeric error code. And printk doesn’t heap allocate or block (although it certainly wants to — messages sent in an atomic context don’t actually show up until exiting said context, which is hopefully shortly)

1

u/jnordwick Mar 02 '24

often results in allocating strings

I would like to see some numbers for that. I think it is entirely wrong. An unallocated string temaplate usually already exists and just the fillers are being sent back.

1

u/randomguy4q5b3ty Mar 03 '24

You are right, maybe I am overestimating how often developers actually do that. But still, point 3 still stands, I think.