Live data from Hacker News

Zig: The Modern Alternative to C

infoworld.com

31–40 of 181 posts

Re: Zig: The Modern Alternative to C

#32
post #8

Earlier quoted context omitted.

The point that paragraph is trying to make but articulates imperfectly is that you explicitly pass an Allocator to the stdlib routines that allocate -- rather than having them allocate on the heap implicitly.

So you have coloured functions? The red that can allocate, and the blue that cannot?

It's not quite as clear-cut, some stdlib modules take an allocator when an 'object' is created (containers mostly work like this), others on each function call that needs to allocate (but crucially, one way or another, an allocator needs to be passed in).

In any case it's not nearly as limiting as the 'red-blue' async-await color split in other languages, and I can't think of a single downside of explicitly passing allocators into stdlib modules instead of having a hidden global allocator.

The approach to always pass allocators isn't all that Zig specific either, it's also a good library design practice in pretty much all other languages which allow explicit memory management.

Re: Zig: The Modern Alternative to C

#33
post #19

That article is about control flow and syntax, which isn't a big problem. What matters is data. So what does Zig have that C doesn't? - Arrays and slices. A slice is a pointer and a length. There's subscript checking on slices. You'd expect that the preferred operation would be to take a slice from a slice, but the documentation does not mention that option. This may be a documentation error. There is a ".." operator…

> No objects, just structs C doesn't have 'objects' either, just structs. In Zig you can add functions to structs though, and you have UFCS syntax sugar, both together is pretty close to class methods ;) Those sentinel terminated arrays are a killer feature for C API interop, since you don't need to allocate separate C strings on the stack or heap just for passing string literals into the C API (Zig string literals a…

Technically C has objects. That's any chunk of memory with a type, size, alignment and duration. Incidentally C++ has exactly the same definition.

Just being pedantic, nothing to see here.

Re: Zig: The Modern Alternative to C

#34

Earlier quoted context omitted.

> No objects, just structs C doesn't have 'objects' either, just structs. In Zig you can add functions to structs though, and you have UFCS syntax sugar, both together is pretty close to class methods ;) Those sentinel terminated arrays are a killer feature for C API interop, since you don't need to allocate separate C strings on the stack or heap just for passing string literals into the C API (Zig string literals a…

Technically C has objects. That's any chunk of memory with a type, size, alignment and duration. Incidentally C++ has exactly the same definition. Just being pedantic, nothing to see here.

C++ has syntax for adding functions to objects and type system support for deriving object types from other object types and overriding those member functions. There are some who consider these a rather important thing.

Re: Zig: The Modern Alternative to C

#35

Earlier quoted context omitted.

> No objects, just structs C doesn't have 'objects' either, just structs. In Zig you can add functions to structs though, and you have UFCS syntax sugar, both together is pretty close to class methods ;) Those sentinel terminated arrays are a killer feature for C API interop, since you don't need to allocate separate C strings on the stack or heap just for passing string literals into the C API (Zig string literals a…

Technically C has objects. That's any chunk of memory with a type, size, alignment and duration. Incidentally C++ has exactly the same definition. Just being pedantic, nothing to see here.

> That's any chunk of memory with a type, size, alignment and duration.

Isn't that just "a variable"?

Re: Zig: The Modern Alternative to C

#36
post #3

What is Zig's async story? Is there something like Go's coroutines?

No, it's 'code transform' async-await as is fashionable these days, but with the advantage that it's 'color blind' (the compiler will do the async transform or not based on the caller's expectation). See: https://kristoff.it/blog/zig-colorblind-async-await/

That's really cool. I'm wondering though how Zig handles massive concurrency? Is there some possibility to have greenthreading?

Re: Zig: The Modern Alternative to C

#37
post #34

Earlier quoted context omitted.

Technically C has objects. That's any chunk of memory with a type, size, alignment and duration. Incidentally C++ has exactly the same definition. Just being pedantic, nothing to see here.

C++ has syntax for adding functions to objects and type system support for deriving object types from other object types and overriding those member functions. There are some who consider these a rather important thing.

I'm just saying that, pedantically, "object" is a term of art in C and C++ which has little to do with the typical OOP definition of object.

Re: Zig: The Modern Alternative to C

#38

Earlier quoted context omitted.

Technically C has objects. That's any chunk of memory with a type, size, alignment and duration. Incidentally C++ has exactly the same definition. Just being pedantic, nothing to see here.

> That's any chunk of memory with a type, size, alignment and duration. Isn't that just "a variable"?

  int * int_ptr = malloc(sizeof(int));
int_ptr is a variable (and also an object), the pointed to memory is an object but not a variable.

Re: Zig: The Modern Alternative to C

#39
post #13
post #5

Is it a good alternative to Go? How is interoperability?

It is not an alternative to Go same as C++ or Rust is not really an alternative to Go. They are on a different level, but can do the same job. Though I could argue that Tcl could be an alternative to Go, I think many would not agree. Zig has manual memory management, doesn't have much of a runtime (no CSP out of the box) and above all is not mature yet (breaking changes do happen). As of now it has one of the best st…

I would be interested in hearing more about your point.

TCL is an elegant scripting language, very LISP-y, where most data structures are lists of strings.

Go on the other hand is more like a modern mix of C and Pascal with GC and lots of convenient libraries, and which builds statically.

The performance of TCL and Go are also a world apart.

Re: Zig: The Modern Alternative to C

#40

Earlier quoted context omitted.

Technically C has objects. That's any chunk of memory with a type, size, alignment and duration. Incidentally C++ has exactly the same definition. Just being pedantic, nothing to see here.

> That's any chunk of memory with a type, size, alignment and duration. Isn't that just "a variable"?

I think the point is that the C standard (or K&R, or both? I forget) repeatedly refers to "objects", and uses the word differently than we're used to in OOP
Post reply on HN