News for hackers. Stuff that matters.
Re:Maintain It with Zig by oconnor663 on Wednesday September 08, 2021 @01:10PM (#28460044)
> Would it be fair to say that Zig is to C, what Rust is to C++?

There's definitely something to that analogy, but I think it also misses a lot of really important details. For example, Zig has generics, which right off the bat makes it hard to say that it's "like C". Also Rust enforces memory safety, which isn't like C or C++.

Re:Maintain It with Zig by roblabla on Wednesday September 08, 2021 @01:43PM (#28460526)
I mean, C11 also has generics, but I would really suggest not using it X). See https://en.cppreference.com/w/c/language/generic

Of couse, it's a much less powerful feature, there's nothing like interfaces or stuff, just a way to "overload" based on the type.

Re:Maintain It with Zig by yisonPylkita on Wednesday September 08, 2021 @03:51PM (#28462164)
Thank you. I had no idea that you could do such a thing in C11 preprocessor
Re:Maintain It with Zig by flohofwoe on Thursday September 09, 2021 @04:54AM (#28467698)
_Generic() is not a preprocessor feature, but a "real" language feature (I was confused too when I saw it first though).

The only preprocessor part in the example is mapping the function-like macro 'cbrt(X)' to the language keyword '_Generic(X)'.

Re:Maintain It with Zig by woodrowbarlow on Wednesday September 08, 2021 @02:48PM (#28461424)
i love that zig doesn't have a special syntax for generics, it just allows anything to be resolved at compile-time -- including types. which gives you generics 'for free'.
Re:Maintain It with Zig by maleldil on Wednesday September 08, 2021 @04:50PM (#28462886)
I've only cursory of Zig, so bear with me. Does this mean that Zig templates are like C++, that is, duck typing? Or is there a notion of constraining the type arguments?
Re:Maintain It with Zig by kristoff_it on Wednesday September 08, 2021 @05:07PM (#28463082)
It's duck typed but it's not based on a funky and limited syntax, but rather types at comptime become normal arguments to functions that you can then inspect using normal Zig code.

I wrote a blog post about comptime if you want to learn more: https://kristoff.it/blog/what-is-zig-comptime/

Re:Maintain It with Zig by dnautics on Wednesday September 08, 2021 @06:46PM (#28464002)
It's duck typed but let's say your "template" takes an integer and divides by two depending on some boolean (perhaps you need to return a lookup table of sometimes half the bitwidth of an integer type). That function called by your template at compile time to maybe-divide-by-two is in the same language as runtime zig, and you could even conceivably call that very same function as a compiled entity in the runtime context.
Re:Maintain It with Zig by robinei on Wednesday September 08, 2021 @05:07PM (#28463080)
You can constrain type arguments with a compile-time `if` check, and generate a compile error on violation.