April 30, 2006
>> In pseudo-code ...
>> 
>>    foreach (inout Customer cust; CustomerSet )
>>    {
>>        try {
>>        cust.name = UI.CustName;
>>        cust.address = UI.Address;
>>        . . .
>>        }
>>        catch (BadUI e)
>>        {
>>            // Recover from the (rare) UI data error
>>            . . .
>>            retry; // Reprocess the same customer record.
>>        }
>>    }
>> 
>
>Does that retry that instance, or retry the entire loop? Couldn't the semantics be either, given the appropriate condition?

Fortunately, it's not ambiguous.  The try/catch is INSIDE the loop, so it will continue the loop where it left off once the error is handled.  If the try/catch were outside the loop, it would run the whole loop over again.

I don't think it's really that difficult; and no, you can't separate the try/catch and have one in and one out.  That's a syntax error.  If you throw errors and catch them elsewhere, my understanding is that the program *should* resume at the end of the try block once it's handled.  Please correct me if I'm wrong.

Also, unless some method is throwing an error, I highly recommend simply using if or assert instead of try/catch.  I *think* try/catch is traditionally done through a very time consuming system fault mechanism, whereas if/assert are simply a "short jcc" instruction.


April 30, 2006
Bruno Medeiros wrote:
> 
> The remaining case, where you want to use continues and retries in the same for, well, I don't think it's a common enough case that makes it worth the introduction of a new keyword just some trivial syntactic sugar.
> In fact, the very idea seems like a very awkward idiom to me. I would like to examine a real example, can someone post one?

Deleting selected members of a sequence:

    for( int i = 0; i < a.length; ++i )
    {
        if( isMatch( a[i] ) )
        {
            swap( a[i], a[$-1] );
            a.length = a.length - 1;
            retry;
        }
    }


Sean
April 30, 2006
Derek Parnell wrote:
> On Sun, 30 Apr 2006 10:28:13 +1000, kris <foo@bar.com> wrote:
> 
>> Derek Parnell wrote:
>>
>>> On Sun, 30 Apr 2006 06:02:10 +1000, Bruno Medeiros   <brunodomedeirosATgmail@SPAM.com> wrote:
>>>
>>>> In fact, the very idea seems like a very awkward idiom to me. I would   like to examine a real example, can someone post one?
>>>
>>>   We use it in the area of retrying a database transaction after some  sort  of exception condition has happened. It has nothing directly to  do with  loop index maintenance.
>>>  In pseudo-code ...
>>>     foreach (inout Customer cust; CustomerSet )
>>>    {
>>>        try {
>>>        cust.name = UI.CustName;
>>>        cust.address = UI.Address;
>>>        . . .
>>>        }
>>>        catch (BadUI e)
>>>        {
>>>            // Recover from the (rare) UI data error
>>>            . . .
>>>            retry; // Reprocess the same customer record.
>>>        }
>>>    }
>>>
>>
>> Does that retry that instance, or retry the entire loop? Couldn't the  semantics be either, given the appropriate condition?
> 
> 
> Just the instance and not the entire loop. There is a 'restart' key word  to do the whole loop thing.
> 
> 


Thanks; I had missed the 'restart';

- Kris
April 30, 2006
I would probably do something more like (assuming there is no other code but the tests):

int i = 0;
while (i < 10)
{
    if (string[i] == something)
    {
        i += some_other_function();
        continue;
    }
    else if (string[i] == something_else)
    {
        i += some_other_function2();
        continue;
    }

    // Otherwise, simply execute the "i++" and re-test
    i++;
}

I mean, while is a keyword too, isn't it?  I really am unclear on how it's any more obtuse... perhaps you're just not as used to it?

Do you cut everything, even cheese and fruit, with a steak knife? Perhaps you do.  But do master chefs as well?  Or do they use the right knives for the right purposes?

Actually, I might do this depending on the number of tests:

int pos = 0;
while (pos < string.length)
{
    switch (string[pos])
    {
    case something:
        pos += some_other_function(string[pos .. string.length]);
        break;

    case something_else:
        pos += some_other_function2(string[pos .. string.length]);
        break;

    default:
        pos++;
    }
}

Which, to me, seems much more clear than any "retry" would.  In fact, "retry" would seem incredibly unclear to me.  That's just me.  I don't see the concept of "retrying" anywhere.

The above code looks somewhat similar to code I've used in an xml document parser and a simple abbreviated xpath expression evaluator.  I can't even remember the last time I've used/wanted anything like a "retry".

Clearly, just my opinion.

-[Unknown]


> Here's an idea:
> 
> There should be a way in D to allow the reconsideration of a for..loop test
> clause without executing the increment clause.
> 
> Using the terminology:
> for (initialize-clause; conditional-clause; increment-clause)
> 
> Example:
> int i;
> for (i=0; i<10; i++)
> {
> if (string.substr(i,1) == something)
> {
> i += some_other_function();
> retry;
> }
> else if (string.substr(i,1) == something_else)
> {
> i += some_other_function2();
> retry;
> }
> // Otherwise, simply execute the "i++" and re-test
> }
> 
> I propose the name "retry" for the "retest without increment-clause" command, to
> be used in a manner similar syntax-wise to the way "break" is used today.
> "Retry" would simply bypass the increment-clause and proceed straight to the
> conditional-clause code section, thereby allowing subsequent passes through the
> for loop without the requisite and occasionally unnecessary auto-incrementation.
> 
> It would just be a way to give for loops a little more natural utility without
> having to do some rather obtuse programming techniques, such as using goto's or
> enclosing the code in a while or do loop, etc.
> 
> - Rick C. Hodgin
> 
> 
> 
> int i;
> for (i=0; i<10; i++)
> {
>     if (string.substr(i,1) == something)
>     {
>         i += some_other_function();
>         retry;
>     }
>     else if (string.substr(i,1) == something_else)
>     {
>         i += some_other_function2();
>         retry;
>     }
>     // Otherwise, simply execute the "i++" and re-test
> }
April 30, 2006
Very neat idea.
Several times I've run into situations where this construct would've been very helpful.

Gets my vote!

Rick C. Hodgin wrote:
> Here's an idea:
> 
> There should be a way in D to allow the reconsideration of a for..loop test
> clause without executing the increment clause.
> 
> Using the terminology:
> for (initialize-clause; conditional-clause; increment-clause)
> 
> Example:
> int i;
> for (i=0; i<10; i++)
> {
> if (string.substr(i,1) == something)
> {
> i += some_other_function();
> retry;
> }
> else if (string.substr(i,1) == something_else)
> {
> i += some_other_function2();
> retry;
> }
> // Otherwise, simply execute the "i++" and re-test
> }
> 
> I propose the name "retry" for the "retest without increment-clause" command, to
> be used in a manner similar syntax-wise to the way "break" is used today.
> "Retry" would simply bypass the increment-clause and proceed straight to the
> conditional-clause code section, thereby allowing subsequent passes through the
> for loop without the requisite and occasionally unnecessary auto-incrementation.
> 
> It would just be a way to give for loops a little more natural utility without
> having to do some rather obtuse programming techniques, such as using goto's or
> enclosing the code in a while or do loop, etc.
> 
> - Rick C. Hodgin
> 
> 
April 30, 2006
Unknown W. Brackets wrote:
> I would probably do something more like (assuming there is no other code but the tests):
> 
> int i = 0;
> while (i < 10)
> {
>     if (string[i] == something)
>     {
>         i += some_other_function();
>         continue;
>     }
>     else if (string[i] == something_else)
>     {
>         i += some_other_function2();
>         continue;
>     }
> 
>     // Otherwise, simply execute the "i++" and re-test
>     i++;
> }
> 
> I mean, while is a keyword too, isn't it?  I really am unclear on how it's any more obtuse... perhaps you're just not as used to it?
> 
> Do you cut everything, even cheese and fruit, with a steak knife? Perhaps you do.  But do master chefs as well?  Or do they use the right knives for the right purposes?
> 
> Actually, I might do this depending on the number of tests:
> 
> int pos = 0;
> while (pos < string.length)
> {
>     switch (string[pos])
>     {
>     case something:
>         pos += some_other_function(string[pos .. string.length]);
>         break;
> 
>     case something_else:
>         pos += some_other_function2(string[pos .. string.length]);
>         break;
> 
>     default:
>         pos++;
>     }
> }
> 
> Which, to me, seems much more clear than any "retry" would.  In fact, "retry" would seem incredibly unclear to me.  That's just me.  I don't see the concept of "retrying" anywhere.
> 
> The above code looks somewhat similar to code I've used in an xml document parser and a simple abbreviated xpath expression evaluator.  I can't even remember the last time I've used/wanted anything like a "retry".
> 
> Clearly, just my opinion.
> 
> -[Unknown]
> 
> 
>> Here's an idea:
>>
>> There should be a way in D to allow the reconsideration of a for..loop test
>> clause without executing the increment clause.
>>
>> Using the terminology:
>> for (initialize-clause; conditional-clause; increment-clause)
>>
>> Example:
>> int i;
>> for (i=0; i<10; i++)
>> {
>> if (string.substr(i,1) == something)
>> {
>> i += some_other_function();
>> retry;
>> }
>> else if (string.substr(i,1) == something_else)
>> {
>> i += some_other_function2();
>> retry;
>> }
>> // Otherwise, simply execute the "i++" and re-test
>> }
>>
>> I propose the name "retry" for the "retest without increment-clause" command, to
>> be used in a manner similar syntax-wise to the way "break" is used today.
>> "Retry" would simply bypass the increment-clause and proceed straight to the
>> conditional-clause code section, thereby allowing subsequent passes through the
>> for loop without the requisite and occasionally unnecessary auto-incrementation.
>>
>> It would just be a way to give for loops a little more natural utility without
>> having to do some rather obtuse programming techniques, such as using goto's or
>> enclosing the code in a while or do loop, etc.
>>
>> - Rick C. Hodgin
>>
>>
>>
>> int i;
>> for (i=0; i<10; i++)
>> {
>>     if (string.substr(i,1) == something)
>>     {
>>         i += some_other_function();
>>         retry;
>>     }
>>     else if (string.substr(i,1) == something_else)
>>     {
>>         i += some_other_function2();
>>         retry;
>>     }
>>     // Otherwise, simply execute the "i++" and re-test
>> }

This, to me, is the right solution to the design pattern. No need for a new keyword, and the functionality is clearly the same.
April 30, 2006
Hello all,

I agree with Kyle on this one as well. I have added the "retry" keyword to my parser and it really amounts to the same thing as "continue". The only difference would be if one were to restrict the "retry" keyword to work only within the 'for' and 'foreach' loop constructs. This is a little tough to do with the grammar (though it can be done in the semantic checking phase fairly easily, I believe....Walter [or someone very familiar with the dmdfe] may have to answer that one since my parser doesn't do semantic checking yet ;)

Anyways, easy to add keyword, but in the same place as "continue", so why do it?

My two cents,
Kelly Wilson

In article <e31khr$6tg$1@digitaldaemon.com>, Kyle Furlong says...
>
>Unknown W. Brackets wrote:
>> I would probably do something more like (assuming there is no other code but the tests):
>> 
>> int i = 0;
>> while (i < 10)
>> {
>>     if (string[i] == something)
>>     {
>>         i += some_other_function();
>>         continue;
>>     }
>>     else if (string[i] == something_else)
>>     {
>>         i += some_other_function2();
>>         continue;
>>     }
>> 
>>     // Otherwise, simply execute the "i++" and re-test
>>     i++;
>> }
>> 
>> I mean, while is a keyword too, isn't it?  I really am unclear on how it's any more obtuse... perhaps you're just not as used to it?
>> 
>> Do you cut everything, even cheese and fruit, with a steak knife? Perhaps you do.  But do master chefs as well?  Or do they use the right knives for the right purposes?
>> 
>> Actually, I might do this depending on the number of tests:
>> 
>> int pos = 0;
>> while (pos < string.length)
>> {
>>     switch (string[pos])
>>     {
>>     case something:
>>         pos += some_other_function(string[pos .. string.length]);
>>         break;
>> 
>>     case something_else:
>>         pos += some_other_function2(string[pos .. string.length]);
>>         break;
>> 
>>     default:
>>         pos++;
>>     }
>> }
>> 
>> Which, to me, seems much more clear than any "retry" would.  In fact, "retry" would seem incredibly unclear to me.  That's just me.  I don't see the concept of "retrying" anywhere.
>> 
>> The above code looks somewhat similar to code I've used in an xml document parser and a simple abbreviated xpath expression evaluator.  I can't even remember the last time I've used/wanted anything like a "retry".
>> 
>> Clearly, just my opinion.
>> 
>> -[Unknown]
>> 
>> 
>
>This, to me, is the right solution to the design pattern. No need for a new keyword, and the functionality is clearly the same.


April 30, 2006
On Sun, 30 Apr 2006 20:34:09 +1000, <kellywilson@nowhere.com> wrote:


> Anyways, easy to add keyword, but in the same place as "continue", so why do it?

Because 'continue' and 'retry' are not the same thing. 'continue' means go to the next iteration, and 'retry' means repeat the same iteration.

....CONCEPTUAL CODE....

   for_start:
       init_code;
   for_test:
       if ( end_condition ) goto for_end;
   for_body:
       do_something;
       if <A> goto for_index; // continue
       if <B> goto for_test; // retry
       if <C> goto for_start; // restart
       if <D> goto for_end; // break;
   for_index:
       update_index;
       goto for_test;
   for_end:


-- 
Derek Parnell
Melbourne, Australia
April 30, 2006
Walter Bright wrote:
> Rick C. Hodgin wrote:
> 
>> int i;
>> for (i=0; i<10; i++)
>> {
>>     if (string.substr(i,1) == something)
>>     {
>>         i += some_other_function();
>>         retry;
>>     }
>>     else if (string.substr(i,1) == something_else)
>>     {
>>         i += some_other_function2();
>>         retry;
>>     }
>>     // Otherwise, simply execute the "i++" and re-test
>> }
> 
> 
> I know goto's are evil, but I tend to write such as:
> 
> int i;
> for (i=0; i<10; i++)
> {
>   Lretry:
>     if (string.substr(i,1) == something)
>     {
>         i += some_other_function();
>         goto Lretry;
>     }
>     else if (string.substr(i,1) == something_else)
>     {
>         i += some_other_function2();
>         goto Lretry;
>     }
>     // Otherwise, simply execute the "i++" and re-test
> }

How about we all try to avoid GOTOs and you implement this keyword? :P

Regards,
Alexander Panek
April 30, 2006
Sean Kelly wrote:
> Bruno Medeiros wrote:
>>
>> The remaining case, where you want to use continues and retries in the same for, well, I don't think it's a common enough case that makes it worth the introduction of a new keyword just some trivial syntactic sugar.
>> In fact, the very idea seems like a very awkward idiom to me. I would like to examine a real example, can someone post one?
> 
> Deleting selected members of a sequence:
> 
>     for( int i = 0; i < a.length; ++i )
>     {
>         if( isMatch( a[i] ) )
>         {
>             swap( a[i], a[$-1] );
>             a.length = a.length - 1;
>             retry;
>         }
>     }
> 
> 
> Sean

I was thinking of an example which used continues and retries in the same for, as yours can be cleanly rewritten as the example in my later post.
Anyway, Derek's example serves as a good example of the keyword's usage, but for "foreach", for "for" I'm still finding it a bit odd.


-- 
Bruno Medeiros - CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D