Re: If Statement with Declaration

2017-07-24 Thread Nick Treleaven via Digitalmars-d

On Monday, 24 July 2017 at 13:12:17 UTC, Nick Treleaven wrote:

static if (is(E == enum; alias V)) { // this inserts "V"
   V v;
}

The test comes first as it logically should, as the alias is 
not being used until the following statement.


Hmm, it can be used in the test:

is(Abc U : U*)
is(AA A : A[B], B : int)
is(AA T : T[U], U : const char[])

So the alias part would come first instead:

is(alias U; Abc : U*)
is(alias A; AA : A[B], B : int)
is(alias T; AA : T[U], U : const char[])

Note: The spec page needs to use CamelCase for types (abc -> Abc, 
bar -> Bar), especially as the construct is difficult to read 
anyway. Maybe I'll do this soon. (I think other parts of the spec 
contravene this too).


Re: If Statement with Declaration

2017-07-24 Thread Nick Treleaven via Digitalmars-d

On Monday, 24 July 2017 at 13:12:17 UTC, Nick Treleaven wrote:

On Friday, 21 July 2017 at 21:50:02 UTC, Johan Engelen wrote:

static if (is(E V == enum)) { // this inserts "V"
   V v;
}
```


Yes, but this is a bit different as the goal is to avoid 
repeating V


Sigh. Repeating E. (unintentional ironic mistake)



Re: If Statement with Declaration

2017-07-24 Thread Nick Treleaven via Digitalmars-d

On Friday, 21 July 2017 at 21:50:02 UTC, Johan Engelen wrote:

We do have a construct like that already:
```
static if (is(E V == enum)) { // this inserts "V"
   V v;
}
```


Yes, but this is a bit different as the goal is to avoid 
repeating V (as it may be complex). Repeating a local variable 
name (using e.g. `with...if`) isn't a problem as the variable 
should have a short identifier. With the above `is` expression 
it's unavoidable to have a test and a declaration combined. That 
said, the syntax could be much clearer:


static if (is(E == enum; alias V)) { // this inserts "V"
   V v;
}

This syntax clearly separates the test from the declaration, and 
makes the declaration obvious due to the alias keyword. The test 
comes first as it logically should, as the alias is not being 
used until the following statement. It extends to the other `is` 
expression forms:


is(T; alias A)
is(T : U; alias A)
is(T : U!V, U, V; alias A)
is(T == U!V, U, V; alias A)

(http://dlang.org/spec/expression.html#IsExpression)

Whenever I want to use the identifier variant I always end up 
checking the spec. When I see it in code I usually have to stop 
what I was thinking about to parse the `is` expression.


Re: If Statement with Declaration

2017-07-24 Thread Olivier FAURE via Digitalmars-d
On Friday, 21 July 2017 at 21:32:48 UTC, Andrei Alexandrescu 
wrote:


with (auto r = makeMeARange)
if (!r.empty)
with (auto x = r.front)
{
   ...
}


Andrei


I'm being real nitpicky, but this in particular just seems like a 
slightly worse way to write


with (auto r = makeMeARange)
if (!r.empty)
{
auto x = r.front;
...
}

But yeah, it's cool construct that lends itself to pretty neat 
chaining.


Re: If Statement with Declaration

2017-07-21 Thread Jonathan M Davis via Digitalmars-d
On Friday, July 21, 2017 17:32:48 Andrei Alexandrescu via Digitalmars-d 
wrote:
> On 07/19/2017 09:30 AM, sontung wrote:
> [snip]
>
> This post:
> http://forum.dlang.org/post/vfjlpvpwuyfqoljvp...@forum.dlang.org seems
> to be identical to one on November 3, 2016:
> http://forum.dlang.org/post/dejodpslmjdovstdi...@forum.dlang.org.
>
> Hat tip for persistence!
>
> Regarding the feature itself: it seems to be fancied by the new
> languages, and C++ added it, too. I must be old school because I don't
> see much benefit in it, and don't quite find it natural. It's bizarre
> even lexically: "If the following ... oh wait let me insert some stuff
> ... as I was saying, if the following condition happens..." Conversely,
> I find the let/letrec syntax in functional languages a bit more convivial.

I would have thought that it was a fairly obvious extension given how for
loops work (especially since you can already declare variables in if
statements; you just can't do it and then use something else for the
condition), and it's less verbose than doing something with with, though I
suppose that getting with to work like this would be better than nothing. It
does seem unnecessarily verbose in comparison though.

- Jonathan M Davis



Re: If Statement with Declaration

2017-07-21 Thread Johan Engelen via Digitalmars-d
On Friday, 21 July 2017 at 21:32:48 UTC, Andrei Alexandrescu 
wrote:


It's bizarre even lexically: "If the following ... oh wait let 
me insert some stuff ... as I was saying, if the following 
condition happens..."


(excuse me for muddying the waters)
We do have a construct like that already:
```
static if (is(E V == enum)) { // this inserts "V"
   V v;
}
```

-Johan


Re: If Statement with Declaration

2017-07-21 Thread Andrei Alexandrescu via Digitalmars-d

On 07/19/2017 09:30 AM, sontung wrote:
[snip]

This post: 
http://forum.dlang.org/post/vfjlpvpwuyfqoljvp...@forum.dlang.org seems 
to be identical to one on November 3, 2016: 
http://forum.dlang.org/post/dejodpslmjdovstdi...@forum.dlang.org.


Hat tip for persistence!

Regarding the feature itself: it seems to be fancied by the new 
languages, and C++ added it, too. I must be old school because I don't 
see much benefit in it, and don't quite find it natural. It's bizarre 
even lexically: "If the following ... oh wait let me insert some stuff 
... as I was saying, if the following condition happens..." Conversely, 
I find the let/letrec syntax in functional languages a bit more convivial.


The suggested enhancement of "with" seems to sit well on the page:

with (auto x = fun())
if (x > 0)
{
   ...
}

and offers some additional flexibility such as:

if (!r.empty)
with (auto x = r.front)
{
   ...
}

and also "for free" variations such as:

while (!r.empty)
with (auto x = r.front)
{
   ...
}

and

with (auto r = makeMeARange())
if (!r.empty)
{
   ...
}

Of course there's always the opportunity to go bananas :o).

with (auto r = makeMeARange)
if (!r.empty)
with (auto x = r.front)
{
   ...
}


Andrei


Re: If Statement with Declaration

2017-07-20 Thread Moritz Maxeiner via Digitalmars-d

On Thursday, 20 July 2017 at 14:18:48 UTC, Jack Stouffer wrote:

On Thursday, 20 July 2017 at 14:05:36 UTC, Iakh wrote:

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


Even so, it's already been shown in this thread that the same 
effect can be achieved via a block statement (doing exactly 
what it was designed for)


This decreases readability by splitting up parts that (the 
programmer wants to) semantically belong together.



or with a for loop.


Which is a hack decreasing readability, because it works opposite 
to what one generally expects when reading a looping control 
structure.




It's an small increase in terseness for a decrease in 
readability and an increase in complexity.


W.r.t to the `with` solution only: It's a noticeable increase in 
readability for a minor increase in complexity.


Re: If Statement with Declaration

2017-07-20 Thread Jack Stouffer via Digitalmars-d

On Thursday, 20 July 2017 at 14:05:36 UTC, Iakh wrote:

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


Even so, it's already been shown in this thread that the same 
effect can be achieved via a block statement (doing exactly what 
it was designed for) or with a for loop.


It's an small increase in terseness for a decrease in readability 
and an increase in complexity.


Re: If Statement with Declaration

2017-07-20 Thread Iakh via Digitalmars-d

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.


Re: If Statement with Declaration

2017-07-20 Thread Iakh via Digitalmars-d

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);


Re: If Statement with Declaration

2017-07-20 Thread Moritz Maxeiner via Digitalmars-d

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


Re: If Statement with Declaration

2017-07-20 Thread Guillaume Piolat via Digitalmars-d

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.


Re: If Statement with Declaration

2017-07-20 Thread Olivier FAURE via Digitalmars-d
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.


Re: If Statement with Declaration

2017-07-19 Thread Steven Schveighoffer via Digitalmars-d
I thought I remembered reading about this. But apparently, it was 
*exactly* this.


https://forum.dlang.org/post/dejodpslmjdovstdi...@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


Re: If Statement with Declaration

2017-07-19 Thread Jonathan M Davis via Digitalmars-d
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



Re: If Statement with Declaration

2017-07-19 Thread Steven Schveighoffer via Digitalmars-d

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


Re: If Statement with Declaration

2017-07-19 Thread H. S. Teoh via Digitalmars-d
On Wed, Jul 19, 2017 at 01:30:56PM +, 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


Re: If Statement with Declaration

2017-07-19 Thread jmh530 via Digitalmars-d

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.


Re: If Statement with Declaration

2017-07-19 Thread Dominikus Dittes Scherkl via Digitalmars-d

On Wednesday, 19 July 2017 at 16:49:38 UTC, Jonathan Marler wrote:


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
}


Yes. That's exactly the idea. Only with this synax it's worth it.


Re: If Statement with Declaration

2017-07-19 Thread Jonathan Marler via Digitalmars-d

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
}



Re: If Statement with Declaration

2017-07-19 Thread Dominikus Dittes Scherkl via Digitalmars-d

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


Me too.
Would also allow

with(char c = foo()) switch(x)
{
   // use c
}

so no need to switch over the new declared variable, but still 
beeing able to use it there


Re: If Statement with Declaration

2017-07-19 Thread Swoorup Joshi via Digitalmars-d

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


Re: If Statement with Declaration

2017-07-19 Thread Seb via Digitalmars-d

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.


As Steve mentioned, it's three lines and apart and it makes a 
huge difference if you have to review a large codebase. After 
all, syntactic sugar is one of the reasons why Python is so 
popular.


Re: If Statement with Declaration

2017-07-19 Thread Steven Schveighoffer via Digitalmars-d

On 7/19/17 11:41 AM, 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's 3 lines. One for the extra scope, one for the declaration, and one 
for the closing scope.


Hm... just had an idea:

auto propertyCheck(string condition, T)(T val)
{
static struct Result {
   T _value;
   opCast(B: bool)() {
  mixin("return _value " ~ condition ~ ";");
   }
   alias _value this;
}
return Result(val);
}

if(auto x = someFunc().propertyCheck!" >= 0")
{
   // use x as if it was the result of someFunc()
}

Has some drawbacks, for instance you may want to use the true 
booleanness of whatever T is inside the function.


-Steve


Re: If Statement with Declaration

2017-07-19 Thread Cym13 via Digitalmars-d
On Wednesday, 19 July 2017 at 15:39:02 UTC, Steven Schveighoffer 
wrote:

On 7/19/17 9:30 AM, sontung wrote:

[...]



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.


-Steve


Now that I think about an extension of the comma makes more sense:

if (int i=foo(), i<42) ...

It is exactly the idea that we are trying to convey here (except 
that, deprecated comma aside, it doesn't work because of scope 
issues: i in i<42 is considered undefined).


Given that the comma will never die and that it conveys exactly 
the idea I think think it should be prefered to the semi-colon.


Re: If Statement with Declaration

2017-07-19 Thread Jonathan Marler via Digitalmars-d
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.


-Steve


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
}
}

I imagine this feature allowing it to be rewritten as:

while(auto a = ...;
  auto b = ...;
  something)
{
// use a and b
}




Re: If Statement with Declaration

2017-07-19 Thread Jack Stouffer via Digitalmars-d

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.


Re: If Statement with Declaration

2017-07-19 Thread Steven Schveighoffer via Digitalmars-d

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.


-Steve


Re: If Statement with Declaration

2017-07-19 Thread ag0aep6g via Digitalmars-d

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`.


Re: If Statement with Declaration

2017-07-19 Thread Seb via Digitalmars-d

On Wednesday, 19 July 2017 at 14:46:31 UTC, Andrea Fontana wrote:

On Wednesday, 19 July 2017 at 13:30:56 UTC, sontung wrote:

Thoughts on this sort of feature?


I think I read this topic a couple of times here, and it went 
nowhere...


Andrea


Well someone must take the initiative and submit a DIP for this 
feature.

I miss it a lot too and would try to help this DIP.


Re: If Statement with Declaration

2017-07-19 Thread Jonathan Marler via Digitalmars-d

On Wednesday, 19 July 2017 at 13:30:56 UTC, 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?



Achieving the same semantics as your example today would look 
like this


{
int i = someFunc();
if(i >= 0)
{
// use i
}
}

The disadvantage being that it creates an extra level of nesting. 
I can see how this extra scope can discourage people from 
properly scoping their variables and this feature would eliminate 
the need for the extra scope.


Also note that this example shows how this could be implemented 
using "syntax lowering".


if(  ;  ; ... ; 

Re: If Statement with Declaration

2017-07-19 Thread Andrea Fontana via Digitalmars-d

On Wednesday, 19 July 2017 at 13:30:56 UTC, sontung wrote:

Thoughts on this sort of feature?


I think I read this topic a couple of times here, and it went 
nowhere...


Andrea



Re: If Statement with Declaration

2017-07-19 Thread Atila Neves via Digitalmars-d

On Wednesday, 19 July 2017 at 13:30:56 UTC, 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:


[...]


I like it for C++17 and I like it for D.

Atila


Re: If Statement with Declaration

2017-07-19 Thread Nemanja Boric via Digitalmars-d

On Wednesday, 19 July 2017 at 14:26:52 UTC, Dukc wrote:

On Wednesday, 19 July 2017 at 13:37:50 UTC, Adam D. Ruppe wrote:

I like it.


Me too. I think this should also apply to switch and with 
statements. Perhaps while statements too.


Right, C++17 has it also for a switch:

```
If init-statement is used, the switch statement is equivalent to
{
init_statement
switch ( condition ) statement
}
```

http://en.cppreference.com/w/cpp/language/switch


Re: If Statement with Declaration

2017-07-19 Thread Nemanja Boric via Digitalmars-d

On Wednesday, 19 July 2017 at 13:30:56 UTC, 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?


This is included in C++17: 
http://en.cppreference.com/w/cpp/language/if


```
int demo() {
   if (auto it = m.find(10); it != m.end()) { return it->size(); }
   if (char buf[10]; std::fgets(buf, 10, stdin)) { m[0] += buf; }
...
```


Re: If Statement with Declaration

2017-07-19 Thread Dukc via Digitalmars-d

On Wednesday, 19 July 2017 at 13:37:50 UTC, Adam D. Ruppe wrote:

I like it.


Me too. I think this should also apply to switch and with 
statements. Perhaps while statements too.


Re: If Statement with Declaration

2017-07-19 Thread Adam D. Ruppe via Digitalmars-d

On Wednesday, 19 July 2017 at 13:30:56 UTC, sontung wrote:

Thoughts on this sort of feature?


I like it.


If Statement with Declaration

2017-07-19 Thread sontung via Digitalmars-d
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?


Re: If Statement with Declaration

2016-11-07 Thread ArturG via Digitalmars-d

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?


Re: If Statement with Declaration

2016-11-07 Thread Anonymouse via Digitalmars-d
On Sunday, 6 November 2016 at 05:07:10 UTC, Andrei Alexandrescu 
wrote:

// possible future D
if ((auto variable = fun()) != 42) {
  ...
}

Defining a variable in an expression wouldn't be allowed 
everywhere (but might be contemplated later as an possibility, 
which is a nice thing about this syntax).


I like it but it would have to require the parantheses, or you 
could get ambiguities like:


if (auto variable = noParensGetSomeT == true) {
// is variable of type T or bool, if T can be implicitly cast?
}

A more approachable thing to do is allow variable definitions 
in switch statements:


switch (auto x = fun() { ... }

It is surprising that doesn't work, which is a good argument in 
favor of the feature (removal of an undue limitation, rule of 
least astonishment etc).


This I can get behind, would start using it right away.


Andrei


Re: If Statement with Declaration

2016-11-07 Thread Nick Treleaven via Digitalmars-d
On Sunday, 6 November 2016 at 05:07:10 UTC, Andrei Alexandrescu 
wrote:

if (auto variable = fun(); variable != 42) {
  ...
}

Why does the word "variable" need to appear twice? It seems 
simpler to allow punctuation around existing syntax:


// possible future D
if ((auto variable = fun()) != 42) {
  ...
}


Avoiding stuttering is nice, but maybe it could be made a bit 
clearer if declarations were allowed for ref arguments and we 
used a named function:


alias let = (ref v) => v;

if (let(auto variable = fun()) != 42) {...}

The same feature allows tuple unpacking:

produceTuple.unpack(auto str, int i);

C# has argument declarations. Just an idea. I personally like the 
C++ syntax too as local variable names are usually short and it 
avoids brackets.


Re: If Statement with Declaration

2016-11-06 Thread Enamex via Digitalmars-d
On Sunday, 6 November 2016 at 05:07:10 UTC, Andrei Alexandrescu 
wrote:


The declaration with "if" seems to be a recent fashion. I've 
first seen it in Go and now C++17 took a shine to it - 
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0305r0.html. A DIP would do good to cite that related work.


It seems a low impact feature. Also, the Go/C++ syntaxes seem 
suboptimal to me because they are stuttering:


if variable := fun(); variable != 42 {
  ...
}

or (C++):

if (auto variable = fun(); variable != 42) {
  ...
}

Why does the word "variable" need to appear twice? It seems 
simpler to allow punctuation around existing syntax:


// possible future D
if ((auto variable = fun()) != 42) {
  ...
}

Defining a variable in an expression wouldn't be allowed 
everywhere (but might be contemplated later as an possibility, 
which is a nice thing about this syntax).


Andrei


I remember an old suggestion/DIP allowing 'with' statements to 
introduce/declare symbols/variables.


Might be a cleaner extension to existing language:

with(auto x = f()) if(foo(x)) {}
else with(auto r = root(t)) if(leaf(r) || blah < bar) {}


Re: If Statement with Declaration

2016-11-05 Thread ketmar via Digitalmars-d
On Sunday, 6 November 2016 at 05:07:10 UTC, Andrei Alexandrescu 
wrote:
A more approachable thing to do is allow variable definitions 
in switch statements:


switch (auto x = fun() { ... }

It is surprising that doesn't work, which is a good argument in 
favor of the feature (removal of an undue limitation, rule of 
least astonishment etc).


https://issues.dlang.org/show_bug.cgi?id=11070
lol


Re: If Statement with Declaration

2016-11-05 Thread Andrei Alexandrescu via Digitalmars-d

On 11/5/16 3:52 PM, Jonathan M Davis via Digitalmars-d wrote:

Ultimately though, the biggest hurdle is that someone needs to create a DIP
for it that strongly argues its case with real world examples of how it
would improve code (preferably with code from Phobos and code.dlang.org),
and without a really well-written DIP it's going to be dead in the water


The declaration with "if" seems to be a recent fashion. I've first seen 
it in Go and now C++17 took a shine to it - 
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0305r0.html. A 
DIP would do good to cite that related work.


It seems a low impact feature. Also, the Go/C++ syntaxes seem suboptimal 
to me because they are stuttering:


if variable := fun(); variable != 42 {
  ...
}

or (C++):

if (auto variable = fun(); variable != 42) {
  ...
}

Why does the word "variable" need to appear twice? It seems simpler to 
allow punctuation around existing syntax:


// possible future D
if ((auto variable = fun()) != 42) {
  ...
}

Defining a variable in an expression wouldn't be allowed everywhere (but 
might be contemplated later as an possibility, which is a nice thing 
about this syntax).


A more approachable thing to do is allow variable definitions in switch 
statements:


switch (auto x = fun() { ... }

It is surprising that doesn't work, which is a good argument in favor of 
the feature (removal of an undue limitation, rule of least astonishment 
etc).



Andrei



Re: If Statement with Declaration

2016-11-05 Thread ketmar via Digitalmars-d
On Saturday, 5 November 2016 at 19:52:05 UTC, Jonathan M Davis 
wrote:
and more importantly, I don't know if Walter or Andrei would 
think that it's worth it, and they're the ones who need to be 
convinced.


this addition looks like needless overcomplication even for me. 
and i'm usually collecting feature for aliced without any 
formalities. ;-)


Re: If Statement with Declaration

2016-11-05 Thread Jonathan M Davis via Digitalmars-d
On Thursday, November 03, 2016 22:29:34 Jerry via Digitalmars-d wrote:
>  if(int i = someFunc(); i >= 0)
>  {
>  // use i
>  }
> Thoughts on this sort of feature?

Personally, I'd love to have it, and I think that it's been suggested before
(though I don't remember how that discussion went). I think that it would be
a nice syntactic improvement over what Stefan suggested with an additional
set of braces. It also would work with the else if case, whereas adding an
extra scope would not. And I think that any chance of it actually being
added to the language would hinge on its ability to either make stuff
possible which is not currently possible or on its ability to make stuff
that's really awkward easy to do in a non-awkward way. And there's a better
case to be made that

if(cond)
{
}
else if(auto result = foo(); cond2)
{
}
else if(cond3)
{
}

is harder to emulate with the current language than doing it with the first
if - though even with the first if branch, if you have something like

if(auto result = foo(); cond1)
{
}
else if(cond2)
{
}
else if(cond3)
{
}

the variable created in the first if branch would presumably leave scope
immediately after the condition failed, which would also be much more
difficult to emulate by adding additional braces.

So, maybe it could be argued successfully with a DIP, but in general at this
point, I think that Walter and Andrei are very resistant to making changes
to the language - especially when it's simply for aesthetic benefit. So,
any proposing a feature needs to be able to show that it brings real value.
And while I, personally, think that this brings real value for certain use
cases, I don't know if it brings enough value in general to be worth the
addition, and more importantly, I don't know if Walter or Andrei would think
that it's worth it, and they're the ones who need to be convinced.

Ultimately though, the biggest hurdle is that someone needs to create a DIP
for it that strongly argues its case with real world examples of how it
would improve code (preferably with code from Phobos and code.dlang.org),
and without a really well-written DIP it's going to be dead in the water
unless Walter or Andrei already happened to have wished for something like
that often enough that they didn't take much convincing (which I wouldn't
bet on). Right now, the bar to get a new feature in is pretty high even
simply from the standpoint of the amount of work required to successfully
propose it.

- Jonathan M Davis



Re: If Statement with Declaration

2016-11-05 Thread Jerry via Digitalmars-d
On Friday, 4 November 2016 at 19:26:23 UTC, Steven Schveighoffer 
wrote:

I think it makes it easier to read and it fits with how the
for-statement operates. I write code everyday that could 
utilize this
if-statement syntax, so I thought I might as well bring it up. 
But if
there isn't that much interest in it then I won't bother with 
a DIP.


Please bear in mind that I'm not the gatekeeper, so what I say 
may not be what the actual ones in control think. It's possible 
that Walter and Andrei like the idea and would implement if 
someone fleshed out the proposal. In my experience, I have not 
encountered too many cases (definitely not zero though) where I 
needed such a feature. I can see the utility, and I wouldn't be 
opposed to it.


-Steve


No but you do make a compelling case. Having the scope structured 
the way you did removed the extra indention and it isn't as error 
prone as the other methods. You could also have multiple 
declarations which I don't think work (at least in the C++ 
implementation) for the new if-statement.


{int a; double b; if(func(, ) >= 0)
{
}}


Re: If Statement with Declaration

2016-11-05 Thread Jerry via Digitalmars-d

On Friday, 4 November 2016 at 18:05:51 UTC, Adrian Matoga wrote:

On Friday, 4 November 2016 at 15:34:00 UTC, Jerry wrote:
On Friday, 4 November 2016 at 14:16:44 UTC, Matthias Bentrup 
wrote:

On Thursday, 3 November 2016 at 22:29:34 UTC, Jerry wrote:

if(int i = someFunc(); i >= 0)
{
// use i
}
Thoughts on this sort of feature?


I would prefer the syntax

  if( (int i = someFunc()) >= 0 )
  {
// use i
  }

as this matches the already existing assignment expression 
syntax if you pull the declaration out of the if expression.


Well you can argue the "if( init ; condition )" syntax matches 
"for( init ; condition ; update)", minus the "update" section.


You've just answered your own question:
for (int i = someFunc(); i >= 0; )
{
// use i
break;
}


Yes, it was one of the examples I gave in the first post. Reason 
not to use that is, if you forget "break" you are probably going 
to have an infinite loop. As well you can't use "else" with a for 
statement.




Re: If Statement with Declaration

2016-11-04 Thread Timon Gehr via Digitalmars-d

On 04.11.2016 21:03, Andrej Mitrovic wrote:

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



Not worth the IFTI-induced template bloat IMHO.


Re: If Statement with Declaration

2016-11-04 Thread Andrej Mitrovic via Digitalmars-d

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.


Re: If Statement with Declaration

2016-11-04 Thread Steven Schveighoffer via Digitalmars-d

On 11/4/16 1:46 PM, Jerry wrote:

On Friday, 4 November 2016 at 16:15:21 UTC, Steven Schveighoffer wrote:

It's just a strawman type, I'm sure there's ways to handle these
things, I just didn't put a lot of effort into all the cases.

But really it's just syntax to separate the bool check from the value
itself. You can get more elaborate if you want with the comparison.
The difficult thing is having a type that you use normally, but that
when you use in an if statement, it gives what you want. And declaring
that type in the if clause itself (which is allowed in restricted
circumstances).

This also isn't exactly ideal for things like int, as if(i) would now
be unexpected.


It is just a basic type but ultimately you will probably need more than
one type with a different way of operating to get the same behavior.
That and having part of the code as a string which makes it harder to
read and no syntax highlighting for the code in text editors/ides.

What do you mean if(i) would be unexpected?


I mean something like:

if(auto i = func.cond("==0"))
{
   assert(i == 0);
   if(i)
   {
  writeln("oops!");
   }
}


Sure, it doesn't look great. But my expectation is that your proposal
doesn't pull enough weight to be integrated into the language. There
are enough ways to solve this problem that don't involve language
changes.



It follows the same rules for an if statement condition which an if
statement allows "if(auto i = foo())".


Right, I'm just saying syntax changes like this generally need a high 
motivation and perceived improvement to justify the undertaking.


For instance, we have trusted escapes via this construct:

auto foo = (() @trusted => systemFunc(x))();

It's ugly, verbose, confusing. But it works, and it works today. I'd 
much rather have this look like:


auto foo = @trusted(systemFunc(x));

or something similar, but how much benefit are we going to get from 
this? Is it going to change productivity? Is it going to make code so 
much easier to write that the time taken to implement was worth it? I 
don't know the answer to that. Often the answer to requests for such 
improvements is "yeah, but you can just do it this way, and that's good 
enough". That's all I'm trying to say.



It's syntactic sugar, most of the existing language features don't serve
a functional purpose. They simply serve to simplify another syntax.
Every "foreach" statement could be written as a for-statement. Every
for-statement could be written as a while-statement. And so on so forth.
It doesn't add anything that new either. It follows the same syntax as a
for-statement.


Existence of current sugar is not justification of adding more. Those 
already exist (and BTW have existed for a long long time), there is no 
cost to continuing to have them. There is a cost to adding new features, 
that we must weigh against the benefits.


One thing that could work in your favor is if the change is easy to 
implement in the compiler. That I definitely don't know the answer to.



I think it makes it easier to read and it fits with how the
for-statement operates. I write code everyday that could utilize this
if-statement syntax, so I thought I might as well bring it up. But if
there isn't that much interest in it then I won't bother with a DIP.


Please bear in mind that I'm not the gatekeeper, so what I say may not 
be what the actual ones in control think. It's possible that Walter and 
Andrei like the idea and would implement if someone fleshed out the 
proposal. In my experience, I have not encountered too many cases 
(definitely not zero though) where I needed such a feature. I can see 
the utility, and I wouldn't be opposed to it.


-Steve


Re: If Statement with Declaration

2016-11-04 Thread Adrian Matoga via Digitalmars-d

On Friday, 4 November 2016 at 15:34:00 UTC, Jerry wrote:
On Friday, 4 November 2016 at 14:16:44 UTC, Matthias Bentrup 
wrote:

On Thursday, 3 November 2016 at 22:29:34 UTC, Jerry wrote:

if(int i = someFunc(); i >= 0)
{
// use i
}
Thoughts on this sort of feature?


I would prefer the syntax

  if( (int i = someFunc()) >= 0 )
  {
// use i
  }

as this matches the already existing assignment expression 
syntax if you pull the declaration out of the if expression.


Well you can argue the "if( init ; condition )" syntax matches 
"for( init ; condition ; update)", minus the "update" section.


You've just answered your own question:
for (int i = someFunc(); i >= 0; )
{
// use i
break;
}



Re: If Statement with Declaration

2016-11-04 Thread Jerry via Digitalmars-d
On Friday, 4 November 2016 at 16:15:21 UTC, Steven Schveighoffer 
wrote:
It's just a strawman type, I'm sure there's ways to handle 
these things, I just didn't put a lot of effort into all the 
cases.


But really it's just syntax to separate the bool check from the 
value itself. You can get more elaborate if you want with the 
comparison. The difficult thing is having a type that you use 
normally, but that when you use in an if statement, it gives 
what you want. And declaring that type in the if clause itself 
(which is allowed in restricted circumstances).


This also isn't exactly ideal for things like int, as if(i) 
would now be unexpected.


It is just a basic type but ultimately you will probably need 
more than one type with a different way of operating to get the 
same behavior. That and having part of the code as a string which 
makes it harder to read and no syntax highlighting for the code 
in text editors/ides.


What do you mean if(i) would be unexpected?


I thought you could only declare variables in the first clause?

In any case, I think Stefan really has the best answer:

{int value; if(auto i = someFunc())
{
// ...
}}

Sure, it doesn't look great. But my expectation is that your 
proposal doesn't pull enough weight to be integrated into the 
language. There are enough ways to solve this problem that 
don't involve language changes.


-Steve


It follows the same rules for an if statement condition which an 
if statement allows "if(auto i = foo())".


It's syntactic sugar, most of the existing language features 
don't serve a functional purpose. They simply serve to simplify 
another syntax. Every "foreach" statement could be written as a 
for-statement. Every for-statement could be written as a 
while-statement. And so on so forth. It doesn't add anything that 
new either. It follows the same syntax as a for-statement.


It's not that big of a language feature, it's just a small 
syntactic change, which comes down to. Is this easier to read 
compared to this.


{int value; if(auto i = someFunc())
{
// ...
}}

vs.

if(int value; auto i = someFunc())
{
// ...
}


I think it makes it easier to read and it fits with how the 
for-statement operates. I write code everyday that could utilize 
this if-statement syntax, so I thought I might as well bring it 
up. But if there isn't that much interest in it then I won't 
bother with a DIP.


Re: If Statement with Declaration

2016-11-04 Thread Jerry via Digitalmars-d

On Thursday, 3 November 2016 at 22:29:34 UTC, Jerry wrote:

Thoughts on this sort of feature?


Love your name, Jerry ;)

Maybe something like this psuedo code:

struct Condition(T) {
Condition(T val, bool cond) { m_val = val; m_cond = cond; }
bool opCast(T)() if(is(T == bool)) {
return m_cond;
}

alias m_val this;

private:
T m_val;
bool m_cond;
}

auto cond(T v, bool cond) {
return Condition!T(v, cond);
}


Usage:

int v = 5;
if(v = cond(v, v > 4) {
writeln("Happy coding");
}



Just notice I am not proud of that T.init.


Re: If Statement with Declaration

2016-11-04 Thread Nick Treleaven via Digitalmars-d

On Friday, 4 November 2016 at 13:56:57 UTC, Andrea Fontana wrote:
If you don't like indentation you can simply ignore it or you 
can use old goto :)


{
   int i = someFunc();
   if (i < 0) goto outer;
   // your code here
}
outer:


BTW there is a trick to avoid goto+label:

switch (true) {
default:
  int i = someFunc();
  if (i < 0) break;
  ...
}

If code after goto is long, the reader has to search for the 
label. With switch/break, the reader knows the current scope is 
just skipped, no need to interrupt reading of lines.


We could allow omitting the condition and default label:

switch {
  int i = someFunc();
  if (i < 0) break;
  ...
}

That would encourage the pattern to be used instead of the 
'triangle if' pattern - the rationale for breakable blocks:

https://issues.dlang.org/show_bug.cgi?id=8622


Re: If Statement with Declaration

2016-11-04 Thread Steven Schveighoffer via Digitalmars-d

On 11/4/16 11:27 AM, Jerry wrote:

On Friday, 4 November 2016 at 14:10:51 UTC, Steven Schveighoffer wrote:

Hm... what about something like:

struct BoolCond(T, string cond)
{
   T val;
   bool opCast(B)() if(is(B == bool))
   {
 return mixin("val " ~ cond);
   }
}

auto boolcond(string cond, T)(T val)
{
   return BoolCond!(T, cond)(val);
}

if(auto i = someFunc.boolcond!(">= 0"))
{
   ... // use i
}



Well that's just a basic case, what if you want more than one condition.
Using "&&" for example, or a condition along with another value that's
in the scope.


It's just a strawman type, I'm sure there's ways to handle these things, 
I just didn't put a lot of effort into all the cases.


But really it's just syntax to separate the bool check from the value 
itself. You can get more elaborate if you want with the comparison. The 
difficult thing is having a type that you use normally, but that when 
you use in an if statement, it gives what you want. And declaring that 
type in the if clause itself (which is allowed in restricted circumstances).


This also isn't exactly ideal for things like int, as if(i) would now be 
unexpected.



Then you need another work around for something like this:

if(int value; auto i = someFunc())
{
   // ...
}


I thought you could only declare variables in the first clause?

In any case, I think Stefan really has the best answer:

{int value; if(auto i = someFunc())
{
// ...
}}

Sure, it doesn't look great. But my expectation is that your proposal 
doesn't pull enough weight to be integrated into the language. There are 
enough ways to solve this problem that don't involve language changes.


-Steve


Re: If Statement with Declaration

2016-11-04 Thread Jerry via Digitalmars-d
On Friday, 4 November 2016 at 14:10:51 UTC, Steven Schveighoffer 
wrote:

On 11/3/16 6:29 PM, 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?



Hm... what about something like:

struct BoolCond(T, string cond)
{
   T val;
   bool opCast(B)() if(is(B == bool))
   {
 return mixin("val " ~ cond);
   }
}

auto boolcond(string cond, T)(T val)
{
   return BoolCond!(T, cond)(val);
}

if(auto i = someFunc.boolcond!(">= 0"))
{
   ... // use i
}

-Steve


Well that's just a basic case, what if you want more than one 
condition. Using "&&" for example, or a condition along with 
another value that's in the scope.


void otherFunc(int input)
{
if(auto i = someFunc.boolcond!(">= input")) // --- error
{
   ... // use i
}
}

Then you need another work around for something like this:

if(int value; auto i = someFunc())
{
   // ...
}




Re: If Statement with Declaration

2016-11-04 Thread Jerry via Digitalmars-d
On Friday, 4 November 2016 at 14:16:44 UTC, Matthias Bentrup 
wrote:

On Thursday, 3 November 2016 at 22:29:34 UTC, Jerry wrote:

if(int i = someFunc(); i >= 0)
{
// use i
}
Thoughts on this sort of feature?


I would prefer the syntax

  if( (int i = someFunc()) >= 0 )
  {
// use i
  }

as this matches the already existing assignment expression 
syntax if you pull the declaration out of the if expression.


Well you can argue the "if( init ; condition )" syntax matches 
"for( init ; condition ; update)", minus the "update" section. 
It's cleaner as well as you don't need an extra set of 
parenthesizes. As well that sort of syntax would allow multiple 
declaration. Which I'm not sure that's desired, it becomes 
increasingly messy.


if((int i = someFunc()) >= 0 && (int j = otherFunc()) >= 0)
{
}

You also can't use the declaration as input for someFunc():

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


Re: If Statement with Declaration

2016-11-04 Thread Steven Schveighoffer via Digitalmars-d

On 11/4/16 10:10 AM, Steven Schveighoffer wrote:

On 11/3/16 6:29 PM, 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?



Hm... what about something like:

struct BoolCond(T, string cond)
{
   T val;
   bool opCast(B)() if(is(B == bool))
   {
 return mixin("val " ~ cond);
   }


Of course, I missed this:

 alias val this;

-Steve


Re: If Statement with Declaration

2016-11-04 Thread Matthias Bentrup via Digitalmars-d

On Thursday, 3 November 2016 at 22:29:34 UTC, Jerry wrote:

if(int i = someFunc(); i >= 0)
{
// use i
}
Thoughts on this sort of feature?


I would prefer the syntax

  if( (int i = someFunc()) >= 0 )
  {
// use i
  }

as this matches the already existing assignment expression syntax 
if you pull the declaration out of the if expression.




Re: If Statement with Declaration

2016-11-04 Thread Steven Schveighoffer via Digitalmars-d

On 11/3/16 6:29 PM, 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?



Hm... what about something like:

struct BoolCond(T, string cond)
{
   T val;
   bool opCast(B)() if(is(B == bool))
   {
 return mixin("val " ~ cond);
   }
}

auto boolcond(string cond, T)(T val)
{
   return BoolCond!(T, cond)(val);
}

if(auto i = someFunc.boolcond!(">= 0"))
{
   ... // use i
}

-Steve


Re: If Statement with Declaration

2016-11-04 Thread Andrea Fontana via Digitalmars-d

On Friday, 4 November 2016 at 13:38:30 UTC, Superstar64 wrote:

On Thursday, 3 November 2016 at 22:32:17 UTC, Stefan Koch wrote:

Just Introduce another block
{
  int i = someFunc();
  if (i >= 0) { ... }
}
// i is not visible here


That adds 2 indention levels after formatting.

Unfortunately this doesn't work:
---
{
int i = someFunc();
if (i >= 0):
//your code here
}
// i is not visible here
---


If you don't like indentation you can simply ignore it or you can 
use old goto :)


{
   int i = someFunc();
   if (i < 0) goto outer;
   // your code here
}

outer:
// i is not visibe here






Re: If Statement with Declaration

2016-11-04 Thread Superstar64 via Digitalmars-d

On Thursday, 3 November 2016 at 22:32:17 UTC, Stefan Koch wrote:

Just Introduce another block
{
  int i = someFunc();
  if (i >= 0) { ... }
}
// i is not visible here


That adds 2 indention levels after formatting.

Unfortunately this doesn't work:
---
{
int i = someFunc();
if (i >= 0):
//your code here
}
// i is not visible here
---


Re: If Statement with Declaration

2016-11-04 Thread ixid via Digitalmars-d

On Friday, 4 November 2016 at 00:04:28 UTC, mogu wrote:

Introducing a block is not intuitive and cause an identation.


How is using a block to produce a sub-scope not intuitive? That's 
using a standard feature to do exactly what it is supposed to do.





Re: If Statement with Declaration

2016-11-03 Thread mogu via Digitalmars-d

On Thursday, 3 November 2016 at 22:32:17 UTC, Stefan Koch wrote:

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:


[...]


Just Introduce another block
{
  int i = someFunc();
  if (i >= 0) { ... }
}
// i is not visible here


Introducing a block is not intuitive and cause an identation. As 
a reference, C++17 has adopted a proposal about this, and 
extended to switch:

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0305r1.html



Re: If Statement with Declaration

2016-11-03 Thread Stefan Koch via Digitalmars-d

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:


[...]


Just Introduce another block
{
  int i = someFunc();
  if (i >= 0) { ... }
}
// i is not visible here


If Statement with Declaration

2016-11-03 Thread Jerry via Digitalmars-d


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?