Thread overview
[Issue 3699] New: Feature Request: while-else
Jan 12, 2010
ibrahim YANIKLAR
Jan 12, 2010
ibrahim YANIKLAR
Jan 12, 2010
Simen Kjaeraas
Jan 13, 2010
ibrahim YANIKLAR
Jan 13, 2010
Simen Kjaeraas
Jan 14, 2010
ibrahim YANIKLAR
Jan 15, 2010
Don
January 12, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3699

           Summary: Feature Request: while-else
           Product: D
           Version: unspecified
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: yanikibo@gmail.com


--- Comment #0 from ibrahim YANIKLAR <yanikibo@gmail.com> 2010-01-12 08:39:50 PST ---
It would be nice to add a while-else statement to the language.

while ( Expression1 )
    ScopeStatement1
else
    ScopeStatement2

may act like that:

if ( Expression1 )
    do
        ScopeStatement1
    while ( Expression1 );
else
    ScopeStatement2

or like that:

bool entered = false;

while ( Expression1 )
{
    entered = true;
    ScopeStatement1
}

if (!entered)
    ScopeStatement2


two of them has some problems.
First one is more efficient but you must write the same expression twice.
The second is more readable but there is an extra declaration and an extra
cost.

while-else is more readable and more efficient.

someone can want a python like while-else statement.
using while-finally syntax would be more suitable for that.

while ( ... )
{
    ...
    if ( ... )
       break;
    ...
}
finally
    ScopeStatement3
...

if break occures the ScopeStatement3 is not executed else it is executed once. But I think it is not necessary because we can provide that by using a simple goto statement instead of break.

while ( ... )
{
    ...
    if ( ... )
       goto label;
    ...
}

ScopeStatement3

label:
...

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 12, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3699


Andrei Alexandrescu <andrei@metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrei@metalanguage.com


--- Comment #1 from Andrei Alexandrescu <andrei@metalanguage.com> 2010-01-12 09:19:33 PST ---
What should this do?

if (c1)
    while (c2)
        stmt1
    else
        stmt2

And in particular, how would you reconcile existing code with the new feature?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 12, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3699



--- Comment #2 from ibrahim YANIKLAR <yanikibo@gmail.com> 2010-01-12 10:42:09 PST ---
> What should this do?
> 
> if (c1)
>     while (c2)
>         stmt1
>     else
>         stmt2
> 


if (c1)
{
    while (c2)
        stmt1
    else
        stmt2
}

It should do that of course.

> And in particular, how would you reconcile existing code with the new feature?

In such situations the compiler can warn the developer, perhaps it can be bound to -w switch.

General the developers uses this form:

if (c1)
{
    while (c2)
        stmt1
}
else
    stmt2

instead of:

if (c1)
    while (c2)
        stmt1
else
    stmt2

D2 is in alpha status, isn't it. I see some changes on D2 that breaks code. You should decide whether the new feature is important and necessary enough to accept the risk of code breaks or not.

I usually feel the need for that feature.

an example on fetching rows from db:

while (row = query.nextRow())
    writeln(row);
else
    writeln("no row");

It's really a nice feature. I think D should collect all the nice features itself.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 12, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3699


Simen Kjaeraas <simen.kjaras@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |simen.kjaras@gmail.com


--- Comment #3 from Simen Kjaeraas <simen.kjaras@gmail.com> 2010-01-12 15:16:59 PST ---
(In reply to comment #2)
> D2 is in alpha status, isn't it. I see some changes on D2 that breaks code. You should decide whether the new feature is important and necessary enough to accept the risk of code breaks or not.

Yes. But an important goal of D has been the principle of least astonishment (for C programmers). That is, C code copied to D should either compile successfully, or fail spectacularly. This fails to follow those guidelines, and introduces silent changes in behavior.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 13, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3699



--- Comment #4 from ibrahim YANIKLAR <yanikibo@gmail.com> 2010-01-13 04:16:19 PST ---
(In reply to comment #3)
> This fails to follow those guidelines, and introduces silent changes in behavior.

Not silent, by giving a warning message.

But we can solve this problem by several ways.

1) by giving priority to if.

Examples:

if (c1)
  while (c2)
    stmt1
else
  stmt2

will behave like this:

if (c1)
{
  while (c2)
    stmt1
}
else
  stmt2

----------------------

while (c1)
  while (c2)
    stmt1
  else
    stmt2

will behave like this:

while (c1)
{
  while (c2)
    stmt1
  else
    stmt2
}


(2) in addition to (1) it can be standardized by the rule: the outermost else
is belonging to the outermost while.

Then:

while (c1)
  while (c2)
    stmt1
else
  stmt2

will behave like this:

while (c1)
{
  while (c2)
    stmt1
}
else
  stmt2

(3) in addition to (1) this type of ambiguities (which 'while' 'else' belongs
to) can be forbidden

then this will give an error

while (c1)
  while (c2)
    stmt1
else
  stmt2

(4) using otherwise statement instead of else

while (c1)
  stmt1
otherwise
  stmt2

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 13, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3699



--- Comment #5 from Simen Kjaeraas <simen.kjaras@gmail.com> 2010-01-13 10:39:31 PST ---
(In reply to comment #4)
> (In reply to comment #3)
> > This fails to follow those guidelines, and introduces silent changes in behavior.
> 
> Not silent, by giving a warning message.

"Warnings are not a defined part of the D Programming Language." (http://digitalmars.com/d/2.0/warnings.html) IOW, warnings are not an option.

> But we can solve this problem by several ways.

If that creates inconsistencies, it will not be accepted. And the proposed solutions do.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 14, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3699



--- Comment #6 from ibrahim YANIKLAR <yanikibo@gmail.com> 2010-01-14 04:07:19 PST ---
(In reply to comment #5)
> (In reply to comment #4)
> > (In reply to comment #3)
> > > This fails to follow those guidelines, and introduces silent changes in behavior.
> > 
> > Not silent, by giving a warning message.
> 
> "Warnings are not a defined part of the D Programming Language." (http://digitalmars.com/d/2.0/warnings.html) IOW, warnings are not an option.

OK

> > But we can solve this problem by several ways.
> 
> If that creates inconsistencies, it will not be accepted. And the proposed solutions do.

I think giving priority to 'if' about 'else's solves all inconsistencies between existing code and the new feature. Or the radical solution: 'otherwise' ...

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 15, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3699


Don <clugdbug@yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |clugdbug@yahoo.com.au
           Severity|normal                      |enhancement


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------