If Statement with Declaration
ArturG via Digitalmars-d
digitalmars-d at puremagic.com
Mon Nov 7 10:45:48 PST 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). So
> currently the only way to do something like this in the
> language, that i've found, is to use a for statement.
>
> for(int i = someFunc(); i >= 0;)
> {
> // use i
>
> break;
> }
>
> Not that ideal to use a for statement. It makes it hard to read
> and if the break condition isn't there it might very well be an
> infinite loop. So I was thinking of some sort of syntax like
> this:
>
> if(int i = someFunc(); i >= 0)
> {
> // use i
> }
> Thoughts on this sort of feature?
Hi, i refactored some templates[1] i had, so that you can provide
your own predicate functions.
Variant 1 eager version:
auto when(alias pred, alias fun, T)(T type = T.init)
Variant 2 a lazy version, uses two templates:
auto when(alias pred, T)(T type = T.init)
auto call(alias fun, T)(T type)
check/modify the examples found here
[1] http://melpon.org/wandbox/permlink/g7V0gj4V7SGPjwqL
*OT is dpaste broken (cant seem to create new pastes) or is it
just me?
More information about the Digitalmars-d
mailing list