Why no (auto foo = bar) in while loops?
Andrej Mitrovic
andrej.mitrovich at gmail.com
Wed Aug 24 11:04:47 PDT 2011
Here's some code that iterates through "parents" of some class object
until it finds an object with no parent (where parent is null):
import std.stdio;
class Foo
{
Foo parent;
int state;
this (int state) { this.state = state; }
}
void main()
{
auto foo = new Foo(0);
foo.parent = new Foo(1);
foo.parent.parent = new Foo(2);
while (true)
{
if (auto par = foo.parent)
{
writeln(par.state);
foo = par;
}
else
{
break;
}
}
}
(syntax-highlighted: http://codepad.org/8yHRmICh)
But I was hoping I could simplify this by doing:
while (auto par = foo.parent)
{
writeln(par.state);
foo = par;
}
However that doesn't work, I get back:
expression expected, not 'auto'
Is there a limitation on why this couldn't work or can this be added
to the language?
More information about the Digitalmars-d-learn
mailing list