July 19, 2017
On Wednesday, 19 July 2017 at 15:31:08 UTC, ag0aep6g wrote:
>
> I'd prefer a new variant of `with`:
>
> ----

Looks much cleaner to me.
July 19, 2017
On Wed, Jul 19, 2017 at 01:30:56PM +0000, sontung via Digitalmars-d wrote: [...]
>     if(int i = someFunc(); i >= 0)
>     {
>         // use i
>     }
> Thoughts on this sort of feature?

I've been wanting such a feature for a long time now. Though I'd propose a different syntax:

	if ((int i = someFunc()) >= 0)
	{
		...
	}


T

-- 
I've been around long enough to have seen an endless parade of magic new techniques du jour, most of which purport to remove the necessity of thought about your programming problem.  In the end they wind up contributing one or two pieces to the collective wisdom, and fade away in the rearview mirror. -- Walter Bright
July 19, 2017
On 7/19/17 11:47 AM, Jonathan Marler wrote:
> On Wednesday, 19 July 2017 at 15:39:02 UTC, Steven Schveighoffer wrote:
>> On 7/19/17 9:30 AM, sontung 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?
>>
>>
>> I really like the idea. Only thing I don't like is the possibility for abuse/confusion/errors:
>>
>> if(int i = someFunc(); j >= 0) // typo, or weird relationship, or just intentional obfuscation?
>>
>> It reminds me a bit of why we got rid of the comma operator.
>>
>> This is why I've liked suggestions in the past like:
>>
>> if((int i = foo()) >= 0)
>>
>> That is, you want to use 'if' on an expression while saving the expression, but the if is only looking at a property of that expression.
>>
>> Note this makes if(arr) (the correct meaning, that is ;) much more palatable:
>>
>> if((auto x = getArray()).length)
>>
>> Don't get me wrong, if this syntax is what gets this idea in, I'm fine with it.
>>
>> One possibility is to require usage of the declared variable in the condition.
>>
> 
> I respectfully disagree with this.  I recall many times where I want to declare variables that should be limited to the scope of the conditional block but aren't used in the condition itself, i.e
> 
> {
>      auto a = ...;
>      auto b = ...;
>      while(something)
>      {
>          // use a and b
>      }
> }

This is different. The variable exist outside the scope of the loop. This is more like a for-loop. Arguably, for-loops are better suited for this, and already support it:

for(auto a = ..., b = ...; something;)

An if statement runs once. There isn't an "exists for all the loops" for an if statement. So it's clean and neat to declare them in one place or the other. Only if you need to use it for the condition does the declaration have to be in the condition expression.

But arguably, for loops can be (and have been) abused, so this isn't exactly new territory. Like I said, I'm OK with the proposal if that's what gets it done.

-Steve
July 19, 2017
On Wednesday, July 19, 2017 1:30:56 PM MDT sontung via Digitalmars-d 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?

I've wanted this for years, but based on previous conversations on the topic, I've assumed that we're never getting it. It sure wouldn't hurt my feelings though if someone put forth the time and effort to write a DIP for it. Maybe they could get it accepted. I don't know.

- Jonathan M Davis

July 19, 2017
I thought I remembered reading about this. But apparently, it was *exactly* this.

https://forum.dlang.org/post/dejodpslmjdovstdiwfz@forum.dlang.org

And this is, ... well I guess I continue to have the same ideas :)

https://forum.dlang.org/post/oknvgb$2nr0$1@digitalmars.com
https://forum.dlang.org/post/nvi4t8$1750$1@digitalmars.com

I guess we should see another thread like this in another 8 months?

I remember reading a discussion about using with statements to do this earlier as well, but I can't find it.

-Steve
July 20, 2017
On Wednesday, 19 July 2017 at 20:42:33 UTC, Steven Schveighoffer wrote:
> I remember reading a discussion about using with statements to do this earlier as well, but I can't find it.
>
> -Steve

I don't think this is the discussion you're talking about, but this does bring DIP 1005 to mind:

https://github.com/dlang/DIPs/blob/master/DIPs/DIP1005.md

Personally, I'm in favor of `with` for both variable declarations and imports. It's pretty intuitive semantically.
July 20, 2017
On Wednesday, 19 July 2017 at 13:30:56 UTC, sontung wrote:
> Thoughts on this sort of feature?

This doesn't enable anything new, and breaks readability conventions.
July 20, 2017
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'd be nice to have either of these available in D, though I'd prefer the `with` one, since
- `with` is currently not widely used - usually only for things like `switch (var) with (EnumName) {...}`; this would make `with` pull its own weight (so to speak)
- I think it is way easier to read
July 20, 2017
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 like "with" variant. Very mach like haskells "where". I believe it
would be cool with expressions. Even can emulate named function
arguments

with (const skip_comments = false, const skip_empty_line = true)
auto diff_result = diff(textA, textB, skip_comments, skip_empty_line);
July 20, 2017
On Wednesday, 19 July 2017 at 15:41:18 UTC, Jack Stouffer wrote:
> On Wednesday, 19 July 2017 at 13:30:56 UTC, sontung wrote:
>> Thoughts on this sort of feature?
>
> To be frank, I don't think that helping the programmer reduce the line count in their program by one line is worth further complicating the language.

It is not about reduce number of lines. It is about binding
related things in one statement.