www.digitalmars.com         C & C++   DMDScript  

D - Python inspirations

reply "Matthew Wilson" <dmd synesis.com.au> writes:
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?
Apr 10 2003
next sibling parent reply J C Calvarese <jcc-47 excite.com> writes:
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?
 
 
 
Apr 10 2003
parent reply "Matthew Wilson" <dmd synesis.com.au> writes:
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?
Apr 10 2003
parent Dan Liebgold <Dan_member pathlink.com> writes:
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
Apr 10 2003
prev sibling next sibling parent "Luna Kid" <lunakid neuropolis.org> writes:
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?
Apr 10 2003
prev sibling next sibling parent reply "DDevil" <ddevil functionalfuture.com> writes:
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.
 }
Apr 10 2003
parent reply "Matthew Wilson" <dmd synesis.com.au> writes:
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.
 }
Apr 10 2003
parent "DDevil" <ddevil functionalfuture.com> writes:
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.
Apr 10 2003
prev sibling next sibling parent Patrick Down <pat codemoon.com> writes:
"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.
Apr 10 2003
prev sibling next sibling parent Stephen Waits <steve waits.net> writes:
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
Apr 12 2003
prev sibling parent reply "Walter" <walter digitalmars.com> writes:
"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?
How about: for (int i = 0; i < 10; ++i) { // do some stuff if (condition) { // This only executed if the for loop is terminated with a break statement break; } }
May 23 2003
parent reply "Matthew Wilson" <matthew stlsoft.org> writes:
What if there are multiple breaks?

(I know, I know: local functions)


"Walter" <walter digitalmars.com> wrote in message
news:bampoi$10mn$2 digitaldaemon.com...
 "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?
How about: for (int i = 0; i < 10; ++i) { // do some stuff if (condition) { // This only executed if the for loop is terminated with a break statement break; } }
May 27 2003
parent "Walter" <walter digitalmars.com> writes:
You can use:
    break label;

"Matthew Wilson" <matthew stlsoft.org> wrote in message
news:bb1gip$2j6s$1 digitaldaemon.com...
 What if there are multiple breaks?

 (I know, I know: local functions)


 "Walter" <walter digitalmars.com> wrote in message
 news:bampoi$10mn$2 digitaldaemon.com...
 "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?
How about: for (int i = 0; i < 10; ++i) { // do some stuff if (condition) { // This only executed if the for loop is terminated with a break statement break; } }
Jun 06 2003