C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Bulldoze Exceptions

From: Lorand Szollosi <szollosi.lorand_at_[hidden]>
Date: Mon, 26 Feb 2024 18:13:47 +0800
Hi,

I think you are looking for a continuation monad (specifying a use-case of it), that is, a ‘programmable semicolon’, or a ‘foreach remaining statements in this block). That’d make perfect sense to implement I think: it’s an everyday issue in error handling that you have essentially the same cleanup code after each statement (up to a callee name).

The same thing could bring lazy evaluation (which we don’t currently have for user-defined && and || now).

Regards,
-lorro


> On 26 Feb 2024, at 16:24, Frederick Virchanza Gotham via Std-Proposals <std-proposals_at_[hidden]> wrote:
>
> I reply to Tiago and Jan in series below.
>
>
>> On Sun, Feb 25, 2024 at 3:24 PM Tiago Freire wrote:
>>
>> If I ever saw such a pattern in production code I would be 99.9% that it was bugged even without looking at what else it is doing.
>> Clean up code should never fail.
>
>
> I've seen lots of clean-up code, or finalisation code, over the years
> that can throw exceptions, for example it if opens a file for logging,
> or if it writes to a database, or if it sends data out on the RS232
> port, or even if it just decrements a semaphore. If one part of the
> clean-up or finalisation fails, it's better just to get as much
> clean-up or finalisation done as possible (i.e. don't neglect to write
> to the database just because the RS232 port failed to write).
>
>
>> On Sun, Feb 25, 2024 at 4:17 PM Jan Schultke wrote:
>>
>> However, that really makes the feature no more powerful than literally
>> wrapping everything in a try {...} catch(...) {} block, and you can
>> easily do that with a macro already, or by just writing it out.
>
>
> Yeah that's right, the following code:
>
> { _Bulldoze:
> optional<stringstream> ss;
> ss.emplace();
> if ( ss ) *ss << 77;
> if ( ss ) cout << std::move(*ss).str();
> }
>
> is exactly equivalent to:
>
> { _Bulldoze:
> optional<stringstream> ss;
> try { ss.emplace(); } catch(...){}
> try { if ( ss ) *ss << 77; } catch(...){}
> try { if ( ss ) cout << std::move(*ss).str(); } catch(...){}
> }
>
> This new feature would not make something possible that was previously
> impossible, but it would allow programmers to write simpler, neater
> code.
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2024-02-26 10:14:08