If Statement with Declaration

Andrej Mitrovic via Digitalmars-d digitalmars-d at puremagic.com
Fri Nov 4 13:03:51 PDT 2016


On Thursday, 3 November 2016 at 22:29:34 UTC, Jerry wrote:
>
> So I was thinking of a way of extending if statements that have 
> declarations. The following being as example of the current use 
> of if statements with declarations:
>
>     if(int* weDontPollute = someFunc())
>     {
>          // use weDontPollute
>     }
>
> That's great and all, but it only works by checking if the 
> variable evaluates to true or false. Which is fine for a 
> pointer but otherwise useless for anything else, like integers 
> where zero is usually valid input (index for array).

This is already possible in library code. See here: 
https://github.com/AndrejMitrovic/minilib/blob/510460ff1381f765a66aa3b8f8f6d7e95b4597b9/src/minilib/core/types.d#L88-L137

One could come up with a helper function that encodes the 
condition which is moved into the opCast method. This should be 
trivial to implement.


More information about the Digitalmars-d mailing list