If Statement with Declaration
Jonathan Marler via Digitalmars-d
digitalmars-d at puremagic.com
Wed Jul 19 09:49:38 PDT 2017
On Wednesday, 19 July 2017 at 16:13:28 UTC, Swoorup Joshi wrote:
> On Wednesday, 19 July 2017 at 15:31:08 UTC, ag0aep6g wrote:
>> On 07/19/2017 03:30 PM, sontung wrote:
>>> 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?
>>
>> I'd prefer a new variant of `with`:
>>
>> ----
>> with (int i = someFunc()) if (i >= 0)
>> {
>> // use i
>> }
>> ----
>>
>> It's slightly more verbose, but the meaning is clearer
>> (arguable). It extends automatically to other control
>> structures like `switch`.
>>
>> I wouldn't have this new `with (declaration)` have the magic
>> lookup rules of the existing `with (expression)`. It would be
>> a simpler tool that I'd probably use more than the existing
>> `with`.
>
> I really prefer this over if. This just made sense, just at a
> glance
Could also make 'with' behave like 'if', where it could apply to
a block or just a single statement, i.e.
// 'if' examples
if(x)
doSomething
if(x)
{
doSomething
}
// 'with' examples
with(auto x = 0)
doSomething
with(auto x = 0)
{
doSomething
}
This would automatically make it work with any statement:
with(auto x = 0) if(x)
{
doSomething
}
with(auto x = 0) while(x)
{
doSomething
}
Could also do multiple with statements
with(auto x = 0)
with(auto y = 0)
if(check(x, y))
{
doSomething
}
More information about the Digitalmars-d
mailing list