View mode: basic / threaded / horizontal-split · Log in · Help
November 02, 2005
Re: Dynamic array syntax proposal
Derek Parnell wrote:
> On Wed, 02 Nov 2005 17:53:02 +0100, Ivan Senji wrote:
> 
> 
>>Don Clugston wrote:
>>
>>>Maybe someone else has a different experience?
>>
>>Actually i use , sometimes, it can be usefull,
>>example:
>>
>>while(i=readInt(), i!=0)
>>{
>>}
> 
> 
> 
> Another way for doing this idiom is 
> 
>    while( (i=readInt()) != 0) {}
> 
> However, I always thought that the comma operator was the used to guarantee
> the order of execution of a set of statements. Statements in a block may be
> reordered by the compiler so long as the effect is the same but a
> comma-separated list of statements is never reordered. Is this true or was
> I dreaming?
> 

Really? I have never heard of it. And if it was a dream then it was a 
really strange dream :)
November 02, 2005
Re: Dynamic array syntax proposal
Derek Parnell wrote:
> In effect the syntax for 'for' is 
> 
>     for ( <block>; <booleanexpr> ; <block>) <block>
> 
> so could have ...
> 
>    for( {a=0; i=10;}; a<10; {a++; i--;}) { ... }
> 
> to eliminate commas in the complex cases and leave the simple case as it
> now is.
> 

Sorry, I'm not sure what you're aiming at. Actually I meant that we
should abandon the following syntax:

  http://www.digitalmars.com/d/expression.html#Expression

  Expression: AssignExpression | AssignExpression, Expression

and start supporting the following (this version is by Don Clugston):

  http://www.digitalmars.com/d/statement.html#for

  ForStatement: for(declarations;boolexpression;expr[, expr [,expr ] ])

or something similar. The idea behind this was that once we get rid of
comma as a high level grammar rule, we can fill the gap with new,
convenient functionality. However, I think it looks pretty ugly to use
block brackets inside the for(...)-statement. So D could support
commas inside for-loops (and maybe while-loops too) as a special case.
Like I previously wrote, supporting the comma-syntax everywhere makes
it all too easy to write really obfuscated code (especially inside
conditional (: ?) expressions.
November 02, 2005
Re: Dynamic array syntax proposal
In article <dkan0c$2vgm$1@digitaldaemon.com>, Don Clugston says...
>
..
>IE change syntax of for() to:
>for(declarations; boolexpression; expr[, expr [,expr ] ])
>
..
>
>Maybe someone else has a different experience?

how about:

for(decls [, decls [, decls ] ]; boolexpression; expr[, expr[, expr ] ])
November 02, 2005
Re: Dynamic array syntax proposal
On Wed, 02 Nov 2005 23:19:07 +0200, Jari-Matti Mäkelä wrote:

> Derek Parnell wrote:
>> In effect the syntax for 'for' is 
>> 
>>     for ( <block>; <booleanexpr> ; <block>) <block>
>> 
>> so could have ...
>> 
>>    for( {a=0; i=10;}; a<10; {a++; i--;}) { ... }
>> 
>> to eliminate commas in the complex cases and leave the simple case as it
>> now is.
>> 
> 
> Sorry, I'm not sure what you're aiming at. Actually I meant that we
> should abandon the following syntax:
> 
>    http://www.digitalmars.com/d/expression.html#Expression
> 
>    Expression: AssignExpression | AssignExpression, Expression
> 
> and start supporting the following (this version is by Don Clugston):
> 
>    http://www.digitalmars.com/d/statement.html#for
> 
>    ForStatement: for(declarations;boolexpression;expr[, expr [,expr ] ])
> 
> or something similar. The idea behind this was that once we get rid of
> comma as a high level grammar rule, we can fill the gap with new,
> convenient functionality. However, I think it looks pretty ugly to use
> block brackets inside the for(...)-statement. So D could support
> commas inside for-loops (and maybe while-loops too) as a special case.
> Like I previously wrote, supporting the comma-syntax everywhere makes
> it all too easy to write really obfuscated code (especially inside
> conditional (: ?) expressions.

I wasn't "aiming" for anything really. It was just yet another suggestion
for removing the comma operand from D so that we didn't need special cases
for it. I don't find {} ugly inside 'for' statements, especially as it
would be rarely used. 

If people are really concerned about the comma operand then this is one (of
many possible) ways to remove it from D. Personally, I'm not bothered by
the comma at all. If someone wants to use for tuple element specification
then why not treat that as a special case, or even use a different
delimiter for tuple elements. 

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
3/11/2005 9:37:10 AM
November 02, 2005
Re: Dynamic array syntax proposal
>Comment about b):
>The only regular place I have seen the usage of the comma operator outside
>for-loops is where a {}-block would have done equally well, like:
>
>if (a) b++,c++;
>
>I have actually never seen code that used the comma operator as an expression.
>(Or maybe I have but can't remember.) (this reinforces a) )
>



void fn1();
void fn2();
void fn3();
void fn4();

bool A, B, C, D;
// set A .. D

//this ugly thing
if(A && fn1(), B && fn2(), C && fn3(), D) fn4;

// is the same as this just as ugly but longer thing
if(A)
{
fn1();
if(B)
{
fn2();
if(C)
{
fn3();
if(D)
fn4;
}
}
}
November 03, 2005
Re: Dynamic array syntax proposal
Derek Parnell wrote:
> I wasn't "aiming" for anything really. It was just yet another suggestion
> for removing the comma operand from D so that we didn't need special cases
> for it. I don't find {} ugly inside 'for' statements, especially as it
> would be rarely used. 
> 
> If people are really concerned about the comma operand then this is one (of
> many possible) ways to remove it from D. Personally, I'm not bothered by
> the comma at all. If someone wants to use for tuple element specification
> then why not treat that as a special case, or even use a different
> delimiter for tuple elements. 
> 

Ok, I see your point now. :) I really don't know if the comma-syntax
would be a good way to handle n-dimension arrays. I hope Walter has a
better vision on this. I still hope he doesn't run out of delimiters,
if some other syntax looks better (I think the # and $ are already
reserved for other purposes) ;)
November 03, 2005
Re: Dynamic array syntax proposal
Don Clugston wrote:
> Lionello Lunesu wrote:
> 
>> The comma-syntax is legal because the comma in D behaves just like
>> the 'operator ,' in C/C++: all the expressions are executed and the
>> (return) value is the value of the last expression. Silly example
>> follows:
> 
> 
> IMHO, nearly all uses of the return value of the comma operator are 
> silly. In fact, I'm sure that I have never seen a sensible example.
> The only good use for it that I have seen is by overloading the comma
> operator -- for emulating tuples!
> 
> I'm really surprised that Walter included the comma operator in D. 
> Probably, if was forbidden except in the last part of for()
> statements, nobody would notice it was gone. IE change syntax of
> for() to: for(declarations; boolexpression; expr[, expr [,expr ] ]) 
> and forbid comma everywhere else, freeing it up for something more 
> useful (like tuples).
> 
> Maybe someone else has a different experience?

I think this sounds both reasonable and potentially quite useful.

>> Or maybe a generalization of the current property principle would
>> be enough: a method set_length(int) can already be invoked with
>> set_length = x. Add a 'method' length(int,int) to two dimensional
>> arrays and change the meaning of the comma. This would allow
>> array.length = x,y!
>> 
>> Combine this with another generalization, namely treating return 
>> values as implicit "out" parameters, so that get_length(out int,
>> out int) could be used as x, y = get_length and the tuples are done

Hmm. Possibly this would not bee so hard to implement. And it would give 
some more flexibility to usage, exatly the same way as set_length(int) 
can now be used as set_length = x.

However, this kind of syntactic sugar tends to do its utmost to create 
hurdles for those just learning the language. Which would be a step in C 
direction. So we need proper use cases before any decision can be made.
November 03, 2005
Re: Dynamic array syntax proposal
"Derek Parnell" <derek@psych.ward> wrote in message 
news:e3dbkps2rk2b.6r0ugtlyce9l.dlg@40tude.net...
> On Wed, 02 Nov 2005 18:44:49 +0200, Jari-Matti Mäkelä wrote:
>
>
> [snip]
>>
>> But it's nice to have multiple expressions in for/while-loops:
>>
>>    for(a=0; a<10; a++, i--) { ... }
>>
>
> In effect the syntax for 'for' is
>
>    for ( <block>; <booleanexpr> ; <block>) <block>
>
> so could have ...
>
>   for( {a=0; i=10;}; a<10; {a++; i--;}) { ... }
>
> to eliminate commas in the complex cases and leave the simple case as it
> now is.


//or:
a=0; i=10;
while (a<10)
{
...
a++; i--;
}

If one really wanted to use a comma in a for-statement, there's always 
'while' as an alternative. No need for "{}" inside the for.

L.
November 03, 2005
Re: Dynamic array syntax proposal
On Thu, 3 Nov 2005 09:39:32 +0200, Lionello Lunesu wrote:

> "Derek Parnell" <derek@psych.ward> wrote in message 
> news:e3dbkps2rk2b.6r0ugtlyce9l.dlg@40tude.net...
>> On Wed, 02 Nov 2005 18:44:49 +0200, Jari-Matti Mäkelä wrote:
>>
>>
>> [snip]
>>>
>>> But it's nice to have multiple expressions in for/while-loops:
>>>
>>>    for(a=0; a<10; a++, i--) { ... }
>>>
>>
>> In effect the syntax for 'for' is
>>
>>    for ( <block>; <booleanexpr> ; <block>) <block>
>>
>> so could have ...
>>
>>   for( {a=0; i=10;}; a<10; {a++; i--;}) { ... }
>>
>> to eliminate commas in the complex cases and leave the simple case as it
>> now is.
> 
> //or:
> a=0; i=10;
> while (a<10)
> {
> ...
> a++; i--;
> }
> 
> If one really wanted to use a comma in a for-statement, there's always 
> 'while' as an alternative. No need for "{}" inside the for.

This is true. And to retain the scoping aspects of the 'for'...

{ // Begin a new scope block
 int a=0;
 int i=10;
 while (a<10)
 {
   ...
  a++; i--;
 }
}

But this starts to lose the simplicity of the 'for'.
-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
3/11/2005 6:50:13 PM
November 03, 2005
Re: Dynamic array syntax proposal
In article <1moow0ntydg9i$.1r99qh45tgq2m$.dlg@40tude.net>, Derek Parnell says...
>
>On Thu, 3 Nov 2005 09:39:32 +0200, Lionello Lunesu wrote:
>
>> "Derek Parnell" <derek@psych.ward> wrote in message 
>> news:e3dbkps2rk2b.6r0ugtlyce9l.dlg@40tude.net...
>>> On Wed, 02 Nov 2005 18:44:49 +0200, Jari-Matti Mäkelä wrote:
>>>
>>>
>>> [snip]
>>>>
>>> In effect the syntax for 'for' is
>>>
>>>    for ( <block>; <booleanexpr> ; <block>) <block>
>>>
>>> so could have ...
>>>
>>>   for( {a=0; i=10;}; a<10; {a++; i--;}) { ... }
>>>
>>> to eliminate commas in the complex cases and leave the simple case as it
>>> now is.

The problem with the first block is when you want to declare variables:
{int a = 0; int i = 10}... (They go out of scope)

>> If one really wanted to use a comma in a for-statement, there's always 
>> 'while' as an alternative. No need for "{}" inside the for.
>
>This is true. And to retain the scoping aspects of the 'for'...
>
> { // Begin a new scope block
>  int a=0;
>  int i=10;
>  while (a<10)
>  {
>    ...
>   a++; i--;
>  }
> }
>
>But this starts to lose the simplicity of the 'for'.

And the 'while' alternative also loses the 'continue' semantics.

So use goto cont; instead of continue:

# { // Begin a new scope block
#  int a=0;
#  int i=10;
#  while (a<10)
#  {
#    ...
#   cont: // continue label   
#   a++; i--;
#  }
# }

Neat? :)

Of course, the comma operator helps: :)

# {
#   int a = -1;
#   int i = 11;
#   while(a++,i--,a<10)
#   {
#      ...
#   }
# }

/Oskar
1 2 3
Top | Discussion index | About this forum | D home