C++ Logo

std-proposals

Advanced search

Re: [std-proposals] try else syntax for validity check

From: Thiago Macieira <thiago_at_[hidden]>
Date: Tue, 18 Feb 2025 17:47:01 -0300
On Tuesday, 18 February 2025 17:43:57 Brasilia Standard Time Thiago Macieira
via Std-Proposals wrote:
> On Tuesday, 18 February 2025 17:42:01 Brasilia Standard Time Filip wrote:
> > I know that I can throw meaningful exceptions and check every pointer
> > before dereference. What I want is to help me and others avoid mistaking
> > one or more dereferences. I believe that this is about safety and
> > usability, it is easier to write:
> >
> > try p1->p2->get(0);
> > catch (nullptr) …
>
> The problem is that this syntax is already permitted and you're asking for
> everyone's try blocks to be slower at every pointer that gets dereferenced.
>
> Wouldn't it be better to have a ?-> operator instead?

Actually, this doesn't need a language change. All you need is a library type
whose operator-> throws if it is null. Likewise, it would be possible to
create a monadic expression that becomes a nullable (or disengaged optional)
at the end of the expression.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Principal Engineer - Intel DCAI Platform & System Engineering

Received on 2025-02-18 20:47:06