Whenever I see new C++ features I'm all the more impressed with how Zig manages to provide capabilities similar to -- and at least as powerful as -- type templates, value templates, constexprs, concepts, and conditional compilation (preprocessor), with a single feature and keyword, without any type/template-level programming, a preprocessor or AST macros (i.e. mini-languages different from the ordinary computation language that one has to learn and can make code quite opaque), all in a language that is about as simple as C and can be fully learned in a day or two.
> capabilities similar to -- and at least as powerful as -- type templates, value templates, constexprs, concepts, and conditional compilation (preprocessor), with a single feature and keyword
are you sure ? zig does not seem to have something that looks like concepts (an issue is open : https://github.com/ziglang/zig/issues/1268) nor a way to put restrictions on a given type passed to a function or series of function.
e.g. it seems that what in C++ would be (with concepts, but the same can be written more verbosely with C++03 templates if needed)
void f(IsFoobar auto x) {
}
void g(IsFoobar auto y) {
}
would require in zig to have a comptime test for the "foobar" nature of T in every function, which looks like a downgrade and does not expose the requirements cleanly in the function interface.
../lib/std/debug.zig:221:14: error: unable to evaluate constant expression
if (!ok) unreachable; // assertion failure
^
./example.zig:5:20: note: called from here
comptime assert(std.meta.trait.isSignedInt(@TypeOf(val)));
^
./example.zig:10:19: note: called from here
signedIntsOnly(x);
^
./example.zig:8:26: note: called from here
test "concepts example" {
^
It accomplishes the same thing, just a bit more verbose, since it is using the general tools of the language rather than special syntax.
in C++ but it's quite preferable to refactor this inside the types themselves (for the same reason that you want to refactor any other kind of code in general - besides you don't want to go read the implementation of your functions every time do you ? IDEs show us only prototypes for a reason).
How would you for instance specialize a generic function in Zig ? Does zig accept
> besides you don't want to go read the implementation of your functions every time do you ? IDEs show us only prototypes for a reason
The IDE can be changed to show you assertions in addition to the signature, just as it also shows doc comments. That the signature is special isn't some law of nature, it's just habit (and users of untyped languages don't even have those).
> How would you for instance specialize a generic function in Zig ? ... and properly dispatches between both ?
No, you'd need a single definition with a comptime branch.
> I'd be hard pressed to say that it is "as powerful" given what I'm seeing for now.
What you're seeing is personal taste. In my taste, that Zig doesn't allow overloading and has clear dispatch is clearer is more preferable (in fact, it's touted as one of Zig's biggest strengths); in yours, it is less preferable. That's perfectly OK, but it's got nothing to do with expressive power (which has a pretty good and precise definition: https://youtu.be/43XaZEn2aLc). There is a reason why some languages are more complicated while others are simple -- some people aesthetically prefer the more complex languages while others prefer simpler ones. What is impressive about Zig is that despite being simple, it's not giving up power, at least in those aspects I mentioned. C, another simple language, cannot do those things.
> zig does not seem to have something that looks like concepts ... nor a way to put restrictions on a given type passed to a function or series of function.
Of course it does. Just place a comptime assertion on the @TypeOf a var.
> would require in zig to have a comptime test for the "foobar" nature of T in every function, which looks like a downgrade and does not expose the requirements cleanly in the function interface.
Cleanliness is a matter of personal aesthetic preference, and a rich type specification "in every function" is not less work or less clear than a comptime assertion in every function. In either case you get a clear compilation error. Requirements are never fully specified in the function's interface unless you have dependent types or contracts (both with their own problems and tradeoffs). Even Haskell has partial subroutines. What portions of the spec that is already enforced at compile type should be exposed in the signature -- keeping in mind that it will never be all of them -- is a matter of taste. Zig might shift things around a bit, but the capability to enforce a specification that is at least as strong as structural types at compile-time is already there.
"Target" I guess could mean that it could compile to code for those virtual machines? That would make some sense although for a language that sets out to compete with/replace C it would be a bit strange.
For "consume" I don't know ... do you mean "call into", or something?
Not strange at all, apparently many still aren't aware that C++/CLI exists.
CLR was designed from the start to support C++.
Back in the 2000, "Everything.NET" Microsoft, there was the goal of having everything on top of .NET, it just did not play out that way due to the endless Windows/Office vs .NET politics across MS teams.
Using C++/CLI is much more productive for interop than manually writing P/Invoke declarations.
Or using C++/WinRT to wrap C++ API into COM/UWP libraries.
Likewise on Java, microEJ and Android world, with Android Studio, Eclipse, Netbeans and Oracle Studio you can easily do mixed language development and debugging with C and C++.
Any systems programming language that wants a place at the table on these platforms, needs to cover these workflows as well as C++ does.
With .NET I'd love integration with existing types and generics too.
Actually, I was just thinking aloud. I don't really mean for Zig authors to target .NET, but rather have .NET Core runtime and C# to have the concept of compile-time values and types.
Gee, I would really love something like Python's enumerate() and zip() (but yes, I know that there's alternatives available that are not in the standard library).
> std::format / Python-like formatting library in the Standard Library!
Yes, this is a good addition.
> Ranges / A radical change in how we work with collections!
... but I still have to use verbose warts like std::iota in order to make a collection of integers?
EDIT: no, apparently not. Sanity prevails! (see [1]):
for (int i : std::views::iota{1, 10})
std::cout << i << ' ';
Note for people who don’t see the difference: if a guard doesn’t have a name, it won’t survive until the end of the scope, thus doesn’t guard anything.
Lock guard has no default constructor, so the line will in fact materialize a temporary lock guard around the mutex m, then throw it away at the semi-colon, thus locking nothing.
If it's constructed as a temporary rvalue but never stored or passed as an argument, I think there's places where the compiler can elide the code entirely. Whereas you might prefer it have the system side effects associated with construction and destruction.
No. Sorry for being unclear. [1] is a great video that covers a lot of good stuff in particular this usability bug. It's worth watching in its entirety.
If you don't want to sit through the video, here's the broken code in question:
"unique_lock<mutex>(m_mutex);" has no effect, but you might mistakenly think that this will block on m_mutex. Labeling unique_lock's constructor as [[nodiscard]] would force you to think twice. See [2] for more details.
That's how I felt looking at C++17 deduction guides, and that's even though I've actually been using C++. If you haven't seen them yet you should check them out ;)
C++20 Features https://mcusercontent.com/e93417593cbf4da3dba03d672/files/42...
Language Features of C++17 https://gallery.mailchimp.com/e93417593cbf4da3dba03d672/file...