On Jul 25, 2016 10:45, "D. B." <db0...@gmail.com> wrote:
>
> duplicate of https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/4c0e87f0-5b39-4922-8028-671fd50361b6%40isocpp.org?utm_medium=email&utm_source=footer
>
> searching before posting helps ;-)
How is that a duplicate? That proposal wants different code executed on the various *break* conditions, this on a *continue*. He even mentions that proposal(s).
How is that a duplicate? That proposal wants different code executed on the various *break* conditions, this on a *continue*. He even mentions that proposal(s).
for ( int i = 0; pUnicast != NULL;
pUnicast = pUnicast->Next; i++)
{
Address address( (sockaddr_storage*) pUnicast->Address.lpSockaddr );
if ( !address.IsValid() )
continue; // goto the continue block below
if ( address.IsLoopback() )
continue;
if ( address.GetType() == ADDRESS_IPV6 && !address.IsGlobalUnicast() )
continue;
addresses[numAddresses++] = address;
if ( numAddresses >= maxAddresses )
break;
}
I get the idea, but I really don't like the syntax for it. Especially since it's based on a symptom (ie: how you would write it with goto) rather than the problem.
The actual problem is that there are times when your iteration statement can't be a single expression. So what you want is to have a multi-expression iteration statement. So... do that:
for ( int i = 0; pUnicast != NULL;
pUnicast = pUnicast->Next; i++)
for ( int i = 0; pUnicast != NULL; )
Or just remove the variable i from the loop entirely because it's unused in the loop body, thus reducing the for statement to:for(; pUnicast != nullptr; pUnicat = pUnicast->Next)Not that a multi-statement increment wouldn't be useful, but this code snippet is a pretty terrible motivating example. Sorry Glenn! :)
On Tuesday, July 26, 2016 at 12:01:38 PM UTC+12, Sean Middleditch wrote:Or just remove the variable i from the loop entirely because it's unused in the loop body, thus reducing the for statement to:
for(; pUnicast != nullptr; pUnicat = pUnicast->Next)
Not that a multi-statement increment wouldn't be useful, but this code snippet is a pretty terrible motivating example. Sorry Glenn! :)
It's just some code I saw on reddit. But c'mon, you're being as unimaginative here as I've been lazy in crimping some random code to demonstrate the use case! lolI'm not suggesting the need is desperate, but I have myself a few times in the past written loops where I've realised that I'd like to "continue early" a few times - much like returning early - to avoid complicating later code with logic just to avoid it being hit. But I'd still like to do something a bit more significant on each continue occasionally and not want to repeat that code or to have to try to shoe horn that logic into the increment statement of the loop or use a comma there where it just feels wrong and is too constrained.
I don't think you need to be that imaginative to see that ;) but hey I'm sure you won't be alone in your opinion if you still don't agree. I perhaps should contact the author of the original code and see if they agree if this facility would encourage them to use it over goto. That would a be a little interesting to me.
The thing is that papers proposing language changes always need a pretty good and convincing motivation section that clearly shows how the language feature improves the status-quo. "... I have myself in the past written loops where ..." doesn't really cut it without showing (optimally real world) examples where any other alternative using existing language (or even other proposals) is inferior. You have to convince some 100+ experts that your feature is the way to go and have answers prepared for the inevitable opposition/criticism.
Hi NicolThanks for your comments.
On Tuesday, July 26, 2016 at 3:08:23 AM UTC+12, Nicol Bolas wrote:
I get the idea, but I really don't like the syntax for it. Especially since it's based on a symptom (ie: how you would write it with goto) rather than the problem.
I think you correctly understand the problem statement, but I'm less sure from you're wording that you appreciated what I actually proposed and that I too understand the problem statement.See below:
The actual problem is that there are times when your iteration statement can't be a single expression. So what you want is to have a multi-expression iteration statement. So... do that:
for ( int i = 0; pUnicast != NULL;
pUnicast = pUnicast->Next; i++)
This puts all of the iteration logic in the same place: the `for` statement itself. So if you want to see what happens on each loop, you don't have to look at both the top and the bottom of the `for` statement.
Yes that is the problem, but I don't like the existing for loops syntax, so I'm not sure it's meaningful to propose that back to me, especially when my proposal also catered for the logic being at the top of the loop. See:
for ( int i = 0; pUnicast != NULL; ){continue {pUnicast = pUnicast->Next;++I;}// whatever}
I'm hearing you don't like my proposed syntax. let's find a better one then. You are currently holding onto the existing syntax and I don't find that superior to my suggestion for anything but the most trivial of situations. I'm looking for something that scales better for the non trivial situations.
The thing is that papers proposing language changes always need a pretty good and convincing motivation section that clearly shows how the language feature improves the status-quo. "... I have myself in the past written loops where ..." doesn't really cut it without showing (optimally real world) examples where any other alternative using existing language (or even other proposals) is inferior. You have to convince some 100+ experts that your feature is the way to go and have answers prepared for the inevitable opposition/criticism.
For anyone who has encountered the problem themselves AND sees it as a sufficient problem I'm sure they can imagine a motivating case already. If not, they are simply in the set of people who don't find this issue motivating, so they never will be. Let's be realistic. I'm ok with that. And in any case I'm not going to create an implementation or do any other things that people might have on there wish list, I'm just throwing the idea out there and if that motivates an expert to run with the idea great, if not, that's ok too as I'm sure there time is more valuable than mine. But I don't buy that imagination here is the problem. So you're one of the people unmotivated by the idea, I'm ok with that. But let's not pretend anything would convince you I think you are kidding yourself.
I fail to see how that is in any way better syntax than what I suggested. Indeed, it's worse because it gives the writer the choice of where this "continue" block is.
A reader of a `for` loop should not have to scan through the loop's body just to find out how it iterates through its stuff.
I'm hearing you don't like my proposed syntax. let's find a better one then. You are currently holding onto the existing syntax and I don't find that superior to my suggestion for anything but the most trivial of situations. I'm looking for something that scales better for the non trivial situations.
What is a "non-trivial situation"? That's kinds the problem with your idea: this use case simply doesn't happen *that* often.
`for` is nothing more than syntactic sugar for `while`. But most of us use `for` more often than `while`, so that proves the usefulness of the syntactic sugar. Range-based `for` is the same way; it's exceptionally handy in a lot of cases.
Most uses of `for` loops are just fine with a simple iteration statement. The number of times when a `for` loop gets so complex that you need multiple counter statements (not just expressions but actual statements) is quite low.
In those cases, `goto` is the proper tool. That's why we keep it around, after all. Not because it's useful most of the time. Or some of the time. Or even rarely. But for that 1:10000 case when we have to do something unorthodox, something that our existing abstractions cannot cleanly handle. We take the tool off the shelf and use it to get things done in a reasonable way.
We should not add syntax for exceptionally rare circumstances. Indeed, in code review, if there was no legitimate way to clean up that loop, I would prefer that it be written as a `while` loop, thus forcing the coding style. By doing so, we make it abundantly clear that the loop's logic doesn't follow the standard `for` rules. `for` exists for trivial circumstances; if your circumstances are non-trivial, then it's simply the wrong tool.
On Monday, July 25, 2016 at 9:52:51 PM UTC-4, gmis...@gmail.com wrote:The thing is that papers proposing language changes always need a pretty good and convincing motivation section that clearly shows how the language feature improves the status-quo. "... I have myself in the past written loops where ..." doesn't really cut it without showing (optimally real world) examples where any other alternative using existing language (or even other proposals) is inferior. You have to convince some 100+ experts that your feature is the way to go and have answers prepared for the inevitable opposition/criticism.
For anyone who has encountered the problem themselves AND sees it as a sufficient problem I'm sure they can imagine a motivating case already. If not, they are simply in the set of people who don't find this issue motivating, so they never will be. Let's be realistic. I'm ok with that. And in any case I'm not going to create an implementation or do any other things that people might have on there wish list, I'm just throwing the idea out there and if that motivates an expert to run with the idea great, if not, that's ok too as I'm sure there time is more valuable than mine. But I don't buy that imagination here is the problem. So you're one of the people unmotivated by the idea, I'm ok with that. But let's not pretend anything would convince you I think you are kidding yourself.
So your response to being asked to provide motivation for your proposal is to simply claim that people who are asking for it will never understand. so there's no point in giving it to them. I can't say that I find such an "argument" convincing...