[Issue 4733] Possible bugs caused by dynamic arrays in boolean evaluation context

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Nov 20 11:30:51 UTC 2023


https://issues.dlang.org/show_bug.cgi?id=4733

Nick Treleaven <nick at geany.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |nick at geany.org

--- Comment #38 from Nick Treleaven <nick at geany.org> ---
For the record, this post described why the revert happened:
https://forum.dlang.org/post/mr540j$1533$1@digitalmars.com

"The main reason why it caused issues is this nice idiom:

if(auto arr = someFunction())
{
   // use arr
}

This would HAVE to be split out to two statements, and the arr variable would
be scoped outside of the if statement."

I attempted to make it obsolete:
https://github.com/dlang/dmd/pull/15413

But that was rejected. I still think it would be good to do for editions. If
that is still a no-go then it seems we need something like:

if ((auto arr = expr).ptr)

However, would `arr` then be declared in the `else` branch? If so that is still
not a replacement for the feature.

Another option would be to allow `if (auto arr = expr)` but allow no other uses
of an array as a boolean.

--


More information about the Digitalmars-d-bugs mailing list