Jump to page: 1 2
Thread overview
Python inspirations
Apr 10, 2003
Matthew Wilson
Apr 10, 2003
J C Calvarese
Apr 10, 2003
Matthew Wilson
Apr 11, 2003
Dan Liebgold
Apr 10, 2003
Luna Kid
Apr 11, 2003
DDevil
Apr 11, 2003
Matthew Wilson
Apr 11, 2003
DDevil
Apr 11, 2003
Patrick Down
Apr 12, 2003
Stephen Waits
May 24, 2003
Walter
May 28, 2003
Matthew Wilson
Jun 06, 2003
Walter
April 10, 2003
Being doing Python of late, and am impressed with some of the nice features.

One in particular I've always thought would be nice to have in C or C++ is for-else, as in

for(int i = 0; i < 10; ++i)
{
    // do some stuff

}
else
{
    /// This only called if the for loop is terminated with a break
statement.
}

Any takers for D?



April 10, 2003
You've got my interest.  That sounds useful.  Should we consider
calling it something new like "forelse" for example?  How costly is having a new keyword when it's much easiler for the uninitiated to guess what's going on?

Justin

Matthew Wilson wrote:
> Being doing Python of late, and am impressed with some of the nice features.
> 
> One in particular I've always thought would be nice to have in C or C++ is
> for-else, as in
> 
> for(int i = 0; i < 10; ++i)
> {
>     // do some stuff
> 
> }
> else
> {
>     /// This only called if the for loop is terminated with a break
> statement.
> }
> 
> Any takers for D?
> 
> 
> 

April 10, 2003
I've seen this mentioned somewhere in this NG.
And I sort of like it, too.

Luna Kid


"Matthew Wilson" <dmd@synesis.com.au> wrote in message news:b74p8g$35v$2@digitaldaemon.com...
> Being doing Python of late, and am impressed with some of the nice
features.
>
> One in particular I've always thought would be nice to have in C or C++ is for-else, as in
>
> for(int i = 0; i < 10; ++i)
> {
>     // do some stuff
>
> }
> else
> {
>     /// This only called if the for loop is terminated with a break
> statement.
> }
>
> Any takers for D?
>


April 10, 2003
For me I don't see the need for a new keyword, partly because I'm aware how phobic language designers hate adding reserved words. But also, it seems that else-ing constructs is something that we may want to extend, so else becomes something of a general concept (which it is anyway, I guess).

For example, there's no reason why we wouldn't also use it for `while` as well as `for` as in

while(!b)
{
}
else
{
  // This is called if the while was broken
}


"J C Calvarese" <jcc-47@excite.com> wrote in message news:b74qdr$3rd$1@digitaldaemon.com...
> You've got my interest.  That sounds useful.  Should we consider calling it something new like "forelse" for example?  How costly is having a new keyword when it's much easiler for the uninitiated to guess what's going on?
>
> Justin
>
> Matthew Wilson wrote:
> > Being doing Python of late, and am impressed with some of the nice
features.
> >
> > One in particular I've always thought would be nice to have in C or C++
is
> > for-else, as in
> >
> > for(int i = 0; i < 10; ++i)
> > {
> >     // do some stuff
> >
> > }
> > else
> > {
> >     /// This only called if the for loop is terminated with a break
> > statement.
> > }
> >
> > Any takers for D?
> >
> >
> >
>


April 11, 2003
Why not use the exception mechanism?

try
{
   for (int i = 0; i < 10; i++)
   {
      // Do stuff
      ...
      if (something)
         throw(Error); // Instead of break
   }
}
catch (Error)
{
   // Do stuff here if exception (break)
}

Of course that doesn't look quite as minimalistic, but the behavior is possibly more explicit and clear.

--
// DDevil



On Fri, 11 Apr 2003 07:56:02 +1000, Matthew Wilson wrote:
> Being doing Python of late, and am impressed with some of the nice features.
> 
> One in particular I've always thought would be nice to have in C or C++ is for-else, as in
> 
> for(int i = 0; i < 10; ++i)
> {
>     // do some stuff
> }
> else
> {
>     /// This only called if the for loop is terminated with a break
> statement.
> }
April 11, 2003
Notwithstanding other objections, there's the issue of performance.


"DDevil" <ddevil@functionalfuture.com> wrote in message news:pan.2003.04.11.00.35.05.574731@functionalfuture.com...
> Why not use the exception mechanism?
>
> try
> {
>    for (int i = 0; i < 10; i++)
>    {
>       // Do stuff
>       ...
>       if (something)
>          throw(Error); // Instead of break
>    }
> }
> catch (Error)
> {
>    // Do stuff here if exception (break)
> }
>
> Of course that doesn't look quite as minimalistic, but the behavior is possibly more explicit and clear.
>
> --
> // DDevil
>
>
>
> On Fri, 11 Apr 2003 07:56:02 +1000, Matthew Wilson wrote:
> > Being doing Python of late, and am impressed with some of the nice features.
> >
> > One in particular I've always thought would be nice to have in C or C++ is for-else, as in
> >
> > for(int i = 0; i < 10; ++i)
> > {
> >     // do some stuff
> > }
> > else
> > {
> >     /// This only called if the for loop is terminated with a break
> > statement.
> > }




April 11, 2003
Well then there's always goto (braces added for clarity).

for (int i = 0; i < 10; i++)
{
   // Do stuff
   ...
   if (something)
      goto Error; // Instead of break
}
goto Done;
{
   Error:
   // Do stuff here for the break condition
}

Done:
// Code continues

It's fast anyway.  ;-)

--
// DDevil


On Fri, 11 Apr 2003 11:28:55 +1000, Matthew Wilson wrote:
> Notwithstanding other objections, there's the issue of performance.

April 11, 2003
"Matthew Wilson" <dmd@synesis.com.au> wrote in news:b74p8g$35v$2@digitaldaemon.com:

> One in particular I've always thought would be nice to have in C or C++ is for-else, as in

I'm all for it.  I've made the same sugestion here before.
April 11, 2003
No!  This type of syntax is misleading, especially in the case of the while loop.  It seems like the else would be executed when the condition were *never* true.  For example:

if (test) {
}
else {
// executed when test false
}

versus...

while (test) {
}
else {
// not necessarily executed when test false, instead connected with a break
statement (!!?)
}

The for loop version isn't much better...   I think this can already be done if function literal expressions were extended slightly to allow an immediate call, for example:

thing find_thing (char[] thing_name) {

if (delegate thing(name) { for (int i =0; i < COUNT; ++i) { /* look for name */
return found_thing; }(thing_name)) {
// executed if found
}
else {
// executed if not found
}
}

It looks odd, but it really isn't! ;)     The scoping doesn't change, and you can pass back any type you like (so you could embed such an immediate literal call in a switch for example). The trick is to format the code better....

Dan

In article <b74red$4c8$1@digitaldaemon.com>, Matthew Wilson says...
>
>For me I don't see the need for a new keyword, partly because I'm aware how phobic language designers hate adding reserved words. But also, it seems that else-ing constructs is something that we may want to extend, so else becomes something of a general concept (which it is anyway, I guess).
>
>For example, there's no reason why we wouldn't also use it for `while` as well as `for` as in
>


April 12, 2003
Matthew Wilson wrote:
> 
> One in particular I've always thought would be nice to have in C or C++ is for-else, as in


Matt,

Sadly I have to say this is insanely ugly.

--Steve
« First   ‹ Prev
1 2