Commenti a: Anti-IF idioms in C++ https://www.italiancpp.org/2014/11/23/anti-if-idioms-in-cpp/ Mon, 20 Mar 2017 21:21:21 +0000 hourly 1 https://wordpress.org/?v=4.7.18 Di: marco https://www.italiancpp.org/2014/11/23/anti-if-idioms-in-cpp/#comment-82 Mon, 24 Nov 2014 11:34:06 +0000 http://www.italiancpp.org/?p=3751#comment-82 Happy you’re seeing monads in C++ 🙂 (Bartosz would be proud of you!).

]]>
Di: Nicola https://www.italiancpp.org/2014/11/23/anti-if-idioms-in-cpp/#comment-81 Mon, 24 Nov 2014 10:47:56 +0000 http://www.italiancpp.org/?p=3751#comment-81 Yes, let’s play with the code 🙂

Anyway, just to elaborate on my words about optional: your code too contains an if, but it’s hidden. So if we’re hiding the if away in library code, rather than eliminate it, then you can do it with optional, too, and it’s even better because it’s chainable (and it’s chainable because it’s monadic)

using namespace std::experimental;

template
auto operator>>=(optional const&opt, F&& f) {
if(opt)
return std::forward(f)(*opt);
else
return null_opt;
}

and you use it like.

myopt >>= [](T1 *arg) { return ....; } >>= [](T2 *arg) { return ...; }

The next version of the Foundamentals TS will for sure add a member function to optional to do exactly that, so it will be still easier.

Anyway, the point of your post is true: you can adapt C# code to C++ quite easily with modern features 🙂

]]>
Di: marco https://www.italiancpp.org/2014/11/23/anti-if-idioms-in-cpp/#comment-80 Mon, 24 Nov 2014 08:59:20 +0000 http://www.italiancpp.org/?p=3751#comment-80 PS the real reason for not using the static here is that you want a new Foo instance for each factory object:

Factory factory;
factory.Get(); // new instance
factory.Get(); // reuse
// …
Factory factory2;
factory2.Get(); // new instance
factory2.Get(); // reuse

]]>
Di: marco https://www.italiancpp.org/2014/11/23/anti-if-idioms-in-cpp/#comment-79 Mon, 24 Nov 2014 08:44:51 +0000 http://www.italiancpp.org/?p=3751#comment-79 As I wrote at the beginning (and as Claudio said during his talk), these are just examples of doing the same thing in another way. Sure, we have specific idioms in our favourite language but I think it’s cool that we are able to reuse code from other languages with almost no effort! This means C++ is open to everyone!

About optional, it requires an IF ( e.g. if (opt.is_valid()) ) 🙂 About the static variable I expressly told not to use a static variable! Because it’s too obvious! Let’s play with the code 🙂

]]>
Di: Nicola https://www.italiancpp.org/2014/11/23/anti-if-idioms-in-cpp/#comment-78 Mon, 24 Nov 2014 08:30:42 +0000 http://www.italiancpp.org/?p=3751#comment-78 Great post!

I know Bartosz Milewski is just going to say that the first examples are istances of the Maybe monad! Anyway, the fact that the code directly translates from C# doesn’t mean it is necessarily a good thing to do. In this case for example, object-oriented runtime polymorphism is useless. I think it can be done directly with optional instead of rolling your own IPerformer interface etc…

Regarding the singleton factory: just use a static variable inside the factory function and return it by reference. Statics are thread-safe in C++11, while your example is not, and you don’t have any “if” because is what statics are meant for.

To summarize: if there’s a more “C++-ish” way of doing things, when porting code, I prefer to exploit it 🙂

]]>