if auto and method call

Jethro via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Apr 17 17:48:05 PDT 2017


How to combine the need to localize a result for an if statement 
and have to call a method to get proper comparison:

if (((auto x = X.value()).empty())
{

}

instead of having to do the long and winded way

auto x = X.value();
if (x.empty())
{

}

Many times I need the result of a function inside the if but need 
to check on other value of the function:

auto x = foo();
if (foo().valid())
{
  // use x
}

which should simplify to

if ((auto x = foo()).valid())
{

}

but this code does not work.


I generally need this for regex stuff and it's quite annoying it 
doesn't work.

if (!match(s, "\s*(?P<t>.),").empty())
{
// Need the result of match to do things!
}

but this doesn't work:


if (!(auto r = match(s, "\s*(?P<t>.),")).empty())
{

}


which then requires one to do

auto r = match(s, "\s*(?P<t>.),");
if (!r.empty())
{

}

Doesn't seem like a big deal but seems trivial to implement. We 
can't always do a simple boolean test of the variable we are 
defining:

if (auto x = something)

so we should be able to do

if ((auto x = something).bar())

which is equivalent to

x = something;
if (x.bar())

or even allow

if ((auto x = something) == 3)
{

}






More information about the Digitalmars-d-learn mailing list