Re:C++20 Reference Card
by pron on Wednesday January 29, 2020 @01:04PM
(#22183751)
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.
Re:C++20 Reference Card
by jcelerier on Wednesday January 29, 2020 @03:29PM
(#22185445)
> 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
Re:C++20 Reference Card
by pron on Thursday January 30, 2020 @06:05AM
(#22190526)
> 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.
Re:C++20 Reference Card
by pron on Wednesday January 29, 2020 @03:46PM
(#22185615)
> 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.
Re:C++20 Reference Card
by unwind on Wednesday January 29, 2020 @03:24PM
(#22185398)
What does that even mean?
"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?
Re:C++20 Reference Card
by pjmlp on Thursday January 30, 2020 @04:50AM
(#22190171)
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.
Re:C++20 Reference Card
by lostmsu on Wednesday January 29, 2020 @04:21PM
(#22185977)
Correct on both cases.
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.
Re:C++20 Reference Card
by wyldfire on Wednesday January 29, 2020 @12:16PM
(#22183046)
> Range-based for with initialiser
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 << ' ';
Re:C++20 Reference Card
by dgellow on Wednesday January 29, 2020 @12:58PM
(#22183660)
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.
Re:C++20 Reference Card
by bstamour on Wednesday January 29, 2020 @02:31PM
(#22184819)
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.
Re:C++20 Reference Card
by wyldfire on Wednesday January 29, 2020 @12:25PM
(#22183188)
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.
Re:C++20 Reference Card
by wyldfire on Wednesday January 29, 2020 @12:43PM
(#22183456)
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.
Re:C++20 Reference Card
by mehrdadn on Wednesday January 29, 2020 @01:15PM
(#22183894)
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...
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)
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.well yes, that's my point. You could also write
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
and in another module and properly dispatches between both ?I'd be hard pressed to say that it is "as powerful" given what I'm seeing for now.
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.
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?
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]):
[1] https://en.cppreference.com/w/cpp/ranges/iota_viewI’m working on a Swift Cookbook.
It started as web pages but I’ve moved it to Github. Considering doing an ePub or trying to cram a lot into a one pager.
Having Strings, Sets, Arrays, Dictionaries, and Functional examples on one page might be useful, for example.
Here are my functional examples:
https://github.com/melling/SwiftCookBook/blob/master/functio...
If you know a couple languages, my thinking is that a few pages of examples should be enough to get started in another language.
The same way that int (a); and int a; are equivalent.
So the guard should remain until end of scope, but won't guard anything.
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.[1] https://youtu.be/lkgszkPnV8g?t=1767
[2] http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p177...
I'm trying to gradually replace C++ with Rust as my primary language, but it's still a lot easier to get a well-paying job in C++ than in Rust.