If Statement with Declaration

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Fri Jul 21 14:32:48 PDT 2017


On 07/19/2017 09:30 AM, sontung wrote:
[snip]

This post: 
http://forum.dlang.org/post/vfjlpvpwuyfqoljvpkkw@forum.dlang.org seems 
to be identical to one on November 3, 2016: 
http://forum.dlang.org/post/dejodpslmjdovstdiwfz@forum.dlang.org.

Hat tip for persistence!

Regarding the feature itself: it seems to be fancied by the new 
languages, and C++ added it, too. I must be old school because I don't 
see much benefit in it, and don't quite find it natural. It's bizarre 
even lexically: "If the following ... oh wait let me insert some stuff 
... as I was saying, if the following condition happens..." Conversely, 
I find the let/letrec syntax in functional languages a bit more convivial.

The suggested enhancement of "with" seems to sit well on the page:

with (auto x = fun())
if (x > 0)
{
    ...
}

and offers some additional flexibility such as:

if (!r.empty)
with (auto x = r.front)
{
    ...
}

and also "for free" variations such as:

while (!r.empty)
with (auto x = r.front)
{
    ...
}

and

with (auto r = makeMeARange())
if (!r.empty)
{
    ...
}

Of course there's always the opportunity to go bananas :o).

with (auto r = makeMeARange)
if (!r.empty)
with (auto x = r.front)
{
    ...
}


Andrei


More information about the Digitalmars-d mailing list