Thread overview
auto in foreach not supported by dmd 0.137
Oct 27, 2005
zwang
Oct 27, 2005
Derek Parnell
Oct 27, 2005
zwang
Oct 27, 2005
Derek Parnell
Oct 27, 2005
Ivan Senji
October 27, 2005
import std.stdio;

int main(char[][] args){
	for(auto i=0; i<args.length; ++i) //this works.
		writef(args[i]);
	foreach(auto i; args) //fails to compile
		writef(i);
	
	return 0;
}
October 27, 2005
On Thu, 27 Oct 2005 11:59:11 +0800, zwang wrote:

> import std.stdio;
> 
> int main(char[][] args){
> 	for(auto i=0; i<args.length; ++i) //this works.
> 		writef(args[i]);
> 	foreach(auto i; args) //fails to compile
> 		writef(i);
> 
> 	return 0;
> }

Of course it fails. The 'auto' facility requires a compile-time expression in order to determine the data type. The syntax for the foreach is

  'foreach' '(' [('int' | 'uint') ident,] typeid ident ';' arrayid ')'

There is no place for an 'auto' declaration.

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
27/10/2005 2:56:37 PM
October 27, 2005
Derek Parnell wrote:
> On Thu, 27 Oct 2005 11:59:11 +0800, zwang wrote:
> 
> 
>>import std.stdio;
>>
>>int main(char[][] args){
>>	for(auto i=0; i<args.length; ++i) //this works.
>>		writef(args[i]);
>>	foreach(auto i; args) //fails to compile
>>		writef(i);
>>	
>>	return 0;
>>}
> 
> 
> Of course it fails. The 'auto' facility requires a compile-time expression
> in order to determine the data type. The syntax for the foreach is 
> 
>   'foreach' '(' [('int' | 'uint') ident,] typeid ident ';' arrayid ')' 
> 
> There is no place for an 'auto' declaration.
> 

I see no reason why the data type can not be inferred from arrayid.
October 27, 2005
On Thu, 27 Oct 2005 14:37:17 +0800, zwang wrote:

> Derek Parnell wrote:
>> On Thu, 27 Oct 2005 11:59:11 +0800, zwang wrote:
>> 
>>>import std.stdio;
>>>
>>>int main(char[][] args){
>>>	for(auto i=0; i<args.length; ++i) //this works.
>>>		writef(args[i]);
>>>	foreach(auto i; args) //fails to compile
>>>		writef(i);
>>>
>>>	return 0;
>>>}
>> 
>> Of course it fails. The 'auto' facility requires a compile-time expression in order to determine the data type. The syntax for the foreach is
>> 
>>   'foreach' '(' [('int' | 'uint') ident,] typeid ident ';' arrayid ')'
>> 
>> There is no place for an 'auto' declaration.
>> 
> 
> I see no reason why the data type can not be inferred from arrayid.

I agree. However the 'auto' uses *exactly* the same data type as the expression but foreach needs the element's data type and not the array's data type.

But as you suggest, I have yet to see a foreach in which the element data type is not redundant. We could have had the syntax as  ...

   'foreach' '(' [('int' | 'uint') ident,] ident ';' arrayid ')'

e.g.  foreach(a; args)

and that would still make sense to me. But to overload 'auto' with yet another meaning is starting to appear crazy.

Oh hang on, there is a special case ....

   char[] args;
   foreach( dchar c; args)

where the UTF translation is automated for us.

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
27/10/2005 4:44:11 PM
October 27, 2005
Derek Parnell wrote:
> On Thu, 27 Oct 2005 14:37:17 +0800, zwang wrote:
> 
> 
>>Derek Parnell wrote:
>>
>>>On Thu, 27 Oct 2005 11:59:11 +0800, zwang wrote:
>>>
>>>
>>>>import std.stdio;
>>>>
>>>>int main(char[][] args){
>>>>	for(auto i=0; i<args.length; ++i) //this works.
>>>>		writef(args[i]);
>>>>	foreach(auto i; args) //fails to compile
>>>>		writef(i);
>>>>	
>>>>	return 0;
>>>>}
>>>
>>>Of course it fails. The 'auto' facility requires a compile-time expression
>>>in order to determine the data type. The syntax for the foreach is 
>>>
>>>  'foreach' '(' [('int' | 'uint') ident,] typeid ident ';' arrayid ')' 
>>>
>>>There is no place for an 'auto' declaration.
>>>
>>
>>I see no reason why the data type can not be inferred from arrayid.
> 
> 
> I agree. However the 'auto' uses *exactly* the same data type as the
> expression but foreach needs the element's data type and not the array's
> data type. 
> 
> But as you suggest, I have yet to see a foreach in which the element data
> type is not redundant. We could have had the syntax as  ...
> 
>    'foreach' '(' [('int' | 'uint') ident,] ident ';' arrayid ')' 
> 
> e.g.  foreach(a; args) 
> 
> and that would still make sense to me. But to overload 'auto' with yet
> another meaning is starting to appear crazy.
> 

No sense? But in both these cases the meaning would be the same: infer type from available information. IMO this sytax extension would be quite usefull because in foreach you can know the index and array type at compile time (except in your special case)

By the way, your definition of foreach syntax is wrong, as the index type doesn't need to be int or uint (for example in associative arrays)

> Oh hang on, there is a special case ....
> 
>    char[] args;
>    foreach( dchar c; args)
> 
> where the UTF translation is automated for us.
>