On Thursday, 30 April 2015 at 02:44:22 UTC, Steven Schveighoffer wrote:
On 4/29/15 8:35 PM, Martin Nowak wrote:
Occasionally I'm using if (auto ary = func()), despite the fact that the semantics are wrong, but it's nice and short and works as long a func
always returns null instead of empty slices.

I wonder if it's possible to fix this, as it is, IMO, the only legitimate drawback of this change. Could we make the following work?

if((auto ary = func()).length)

Another thing to consider is how this is a general problem. It's not that atypical to want to declare a variable in the if condition and then use a function call on it for the actual condition, which it looks like your suggestion would work with, but if we consider a solution like that, I think that we need to consider the general case and not just checking for length on arrays.

Another syntax might be something like

if(auto ary = func(); ary.length)

and make it similar to how for loops work. That would also allow you to check stuff other than what the variable gets initialized with or to use it in a more complicated expression. e.g.

if(auto ary = func(); foo(ary))

It's also arguably more consistent with other language features. But regardless, the general idea of what you're proposing could provide some good syntactic sugar.

- Jonathan M Davis

Reply via email to