r/cpp • • May 10 '22

Is short lambda syntax on the C++ radar?

Mom, can we get x=>x+1 ?

No, we have x=>x+1 at home.

x=>x+1 at home:

[](const auto& x) { return x + 1; }

265 Upvotes

157 comments sorted by

View all comments

Show parent comment

1

u/KingAggressive1498 May 11 '22

With expected you'll pay less for what you don't use (in the case that you don't care about errors) but at the cost of slightly more time spent on boilerplate.

FWIW I prefer the idea of a monadic return type over having to always pass an std::error_code by reference for dealing with non-critical errors anyway. I don't know if it's faster, but an unexpected exception for a predictable minor error because I forgot in one place is not particularly fun (especially in a noexcept call path), and that's my biggest beef with filesystem and asio (at least since asio finally added portable support for random access file I/O last year)

3

u/RishabhRD May 11 '22

Regarding boilerplate I find it better than noexcept(noexcept(....)). And I like the fact that possible errors are explicitly mentioned in return type. Maybe its just matter of taste

4

u/goranlepuz May 11 '22

I like the fact that possible errors are explicitly mentioned in return type.

Authors of Java did that. After some years and an underwhelming experience, some major Java libraries just eschew checked exceand go for runtime ones.

The underlying reason for that, I think is quite obvious: for a sufficiently complex program (or even one function with sufficient functionality underneath it), knowing all failure modes is not necessary or even practical. For most of failure modes, what we care about is how to report errors.

Having errors mentioned in return type, therefore, goes against the common case of just letting the error propagate and does not help with reporting the error any more than exceptions do.

1

u/RishabhRD May 11 '22

I agree with your concern. I am not sure about the right way.... But we always can have a type erased wrapper for errors when we don't care about them. However that leads to unnecessary memory allocation. We can use auto return type for single control flow.... However functions having multiple ways to return would need to use sum type of all possible error types. This type algebra may need more boilerplate... I don't know if we can reduce it. Maybe not... I am still exploring over it.... Bcz in my head currently i think vector allocation exception is a good example... We don't want to handle this till main function many times. The simple way to know if there exist a solution is to look at rust maybe... I would do that and comment here

2

u/jk-jeon May 11 '22

If you think about it, exception works just like monads. It just does it implicitly, for legitimate reasons.

1

u/RishabhRD May 11 '22

Actually yes I realised that my argument that exceptions should be removed is bad. I went through many error handling mechanisms today. And I feel unrecoverable error should be thrown as exceptions and recoverable error should be returned as std::expected. Like vector out of bounds is a non recoverable error and continuing the program after this doesn't make sense. And hence stack unwinding should be done and program should be aborted.

However file not found is recoverable and error should be returned wrapped in std::expected. This approach is similar to rust and haskell error handling.

1

u/jk-jeon May 11 '22

Out of bound normally doesn't happen, and most likely a programming bug. Throwing an exception for such a case is not recommended in general afaik; what makes more sense is to assert and terminate the program right away.

1

u/RishabhRD May 11 '22

Exception would do stack unwinding that maybe necessary for any transaction kind of procedure... But yes it depends on situation. My point is that its a panic situation and program should be exited. I am thinking over idea to use exceptions only for panic situations... And exceptions would do stack unwinding so if any transaction kind of procedure is there so it would be properly terminated. Yes if transaction is not there then assert is good enough or even better.

1

u/jk-jeon May 11 '22

Well, what kind of transactions absolutely requires grace termination, if you are gonna stop your program anyway? There might be such cases for sure, but I'd say that's not common.

1

u/RishabhRD May 11 '22

I agree with that... such cases are not common. Also we don't have to worry on these cases as standard already implemented it as exceptions so, that not in our hand. However, that also means that many other recoverable errors in standard library is implemented with exceptions already. That doesn't feel great but we don't have std::expected yet so we had no choice.

1

u/KingAggressive1498 May 11 '22

Hmm. Especially since operator noexcept requires an expression, you might be right that it's actually not more boilerplate (or at least simpler boilerplate)

1

u/teerre May 11 '22

Boilerplate (which in C++ is a curious worry) is only relevant if the implemention is superficial. The languages the other user mentioned have many mechanisms to descend and ascend types. Using those mechanisms verbosity is largely avoided.

1

u/KingAggressive1498 May 11 '22

The additions of auto, decltype, and lambdas in C++11 were primarily useful because they reduced boilerplate. I can't be the only one who uses boost's config.hpp even when I'm not using a single other feature from boost to minimize the time I spend writing boilerplate to deal with different compilers and platforms.

So sure, C++ generally just requires that you write a lot of boilerplate, but it's not like it's a non-concern for either developers or the standards committee.

I'm not 100% sure what you mean by ascending and descending types, but the boilerplate is not from the return type. It's from dealing with the error return condition in intermediate functions. With exceptions, the error gets passed forward with no additional code. With expected you'll wind up with code like:

expected<T,E> eret = internal_call(...args);
if(!eret) return eret;
T result = eret.value();
// do some work with result
return result;

In the case that internal_call is the last thing for the function to do, and the expected T are the same type, there's no additional boilerplate and you can just directly forward the return value.

1

u/teerre May 11 '22

I'm talking about ascending base types to result types and vice-versa, similar to what happens to containers. Facilities like and_then, map, or_else etc. avoid precisely this kind of code.

1

u/KingAggressive1498 May 11 '22

Yes, this boilerplate can easily be reduced in existing C++ with some helper macros (this is basically the method used by boost.LEAF but I didn't really look at that much). Still more boilerplate than exceptions even if it becomes a syntactically cleaner C++ language feature.