Re:A brief introduction to C++'s model for type- and resourc...
by lorenzhs on Saturday January 16, 2016 @07:47AM
(#10915134)
That's a very useful pattern, functional languages often call it the maybe monad. I wish more people used it in other languages. This is my version of it: https://github.com/lorenzhs/algen-framework/blob/master/comm...
Re:A brief introduction to C++'s model for type- and resourc...
by stinos on Saturday January 16, 2016 @09:14AM
(#10915297)
That's actually std::optional, minus the nothing/just notation? Indeed more people should use things like this. Reminds me of a similar concept I also have wrappers for: instead of manually having to compare the results of std::find etc against range.end() all over the place, the result is basically in a small container which has both the iterator returned and the end of the range. So I write
const auto result = alg::find( range, pred );
if( result )
doStuff( *result ); //*result is reference to found element
instead of typcial const auto end = range.cend();
const auto result = std::find( range.cbegin(), end, pred );
if( result != end )
doStuff( *result );
Not that much of a gain, but once used to it I could never go back. And I realize now this could actually just be rewritten using your maybe class or std::optional.
Re:A brief introduction to C++'s model for type- and resourc...
by lorenzhs on Saturday January 16, 2016 @09:37AM
(#10915348)
Yeah it's pretty much std::optional but unfortunately that's in the library fundamentals TS and still in the experimental namespace last I checked. But I have to say that I prefer the name "maybe", it does convey the notion that you have to check whether it's valid much better than "optional" in my opinion.
[1] http://www.llvm.org/docs/doxygen/html/classllvm_1_1ErrorOr.h...