Feature request: extending comma operator's functionality

Tommi tommitissari at hotmail.com
Thu Oct 4 14:56:15 PDT 2012


On Thursday, 4 October 2012 at 21:32:34 UTC, Jonathan M Davis 
wrote:
>
> If you want to restrict the scope of a variable, you can simply 
> use another set of braces to create a new scope. It might be 
> more verbose than desirable, but it works just fine. e.g.
>
> {
>  int n = getInt();
>  if(n > 10)
>  {
>  ...
>  }
> }

But if there are else-if clauses, then you end up polluting your 
namespace, and notice how the syntax of your workaround 
deteriorates exponentially:

The extended if-clause syntax:
------------------------------

if (byte n = fun1(), n > 10)
{
     //...
}
else if (int n = fun2(), n > 100)
{
     //...
}
else if (ulong n = fun3(), n > 1000)
{
     //...
}


The workaround syntax:
----------------------

{
     byte n1 = fun1();
     if (n1 > 10)
     {
         //...
     }
     else
     {
         int n2 = fun2();
         if (n2 > 100)
         {
             //...
         }
         else
         {
             ulong n3 = fun3();
             if (n3 > 1000)
             {
                 //...
             }
         }
     }
}


> As it stands, there's a good chance that the comma operator is 
> actually going to be _removed_ from the language (aside from 
> specific use cases such as inside for loops). So, I don't think
> that there's much chance of it being expanded at all.

I don't see a problem there. I mean, if the comma operator is 
kept in specific cases like inside for loop, why not keep (and 
expand it's use) it in this specific case of if-clause.


More information about the Digitalmars-d mailing list