My new favorite D feature
Pragma
ericanderton at yahoo.removeme.com
Wed Dec 20 07:07:02 PST 2006
alex4u2nv wrote:
> Im new to this language and just getting aquainted, but what happens in the
> scenario of saving and evaluating the return value of a function within the if
> statement to be used elsewhere.
>
> bool search() { //find something; if (..) return index; else return 0;//not found }
>
> if ( retval = search() )
> {
> writefln("found:");
> writefln(retval);
> } else {
> writefln( "not found" );
> }
>
>
> // or something similar?
You have to make it look like a variable declaration at the same time,
thus forcing you to be more explicit:
if(auto retval = search()){ /*...*/ }
The operative word here is 'explicit': it's impossible to confuse with
simply forgetting that extra '='.
However, the only drawback here is that the variable used in the if()
statement is scoped to the if()'s true branch. It's all here:
http://www.digitalmars.com/d/statement.html#IfStatement
--
- EricAnderton at yahoo
More information about the Digitalmars-d
mailing list