Feature request: extending comma operator's functionality
Jonathan M Davis
jmdavisProg at gmx.com
Thu Oct 4 14:26:29 PDT 2012
On Thursday, October 04, 2012 23:11:58 Tommi wrote:
> Could you change it so that expressions, that are separated by
> commas and inside an if-clause, would have visibility to the
> variable defined in the first expression? Easier to show than to
> explain:
>
> int getInt()
> {
> return 11;
> }
>
> void main()
> {
> if (int n = getInt(), n > 10) // undefined identifier n
> {
> //...
> }
>
> if (int n = getInt(), ++n, n > 10) // undefined identifier n
> {
> //...
> }
>
> if (int n = getInt(), getInt() > 10) // OK
> {
> //...
> }
> }
>
> That would make it possible to define variables in the smallest
> possible scope, and not pollute the namespace of the enclosing
> scope.
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)
{
...
}
}
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.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list