www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - auto in foreach not supported by dmd 0.137

reply zwang <nehzgnaw gmail.com> writes:
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;
}
Oct 26 2005
parent reply Derek Parnell <derek psych.ward> writes:
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
Oct 26 2005
parent reply zwang <nehzgnaw gmail.com> writes:
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.
Oct 26 2005
parent reply Derek Parnell <derek psych.ward> writes:
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
Oct 26 2005
parent Ivan Senji <ivan.senji_REMOVE_ _THIS__gmail.com> writes:
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.
 

Oct 27 2005