D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 8625 - foreach doesn't do implicit conversions with opApply
Summary: foreach doesn't do implicit conversions with opApply
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P3 normal
Assignee: No Owner
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2012-09-05 20:41 UTC by Nils
Modified: 2024-12-13 18:01 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Nils 2012-09-05 20:41:43 UTC
This should compile:

void main() {
	int[int] aa;
	foreach(long k, long v; aa) {} // fails on both k and v
		// test.d(3): Error: foreach: index must be type int, not long
		// test.d(3): Error: foreach: value must be type int, not long
	
	static struct OpApply {
		int opApply(int delegate(int)) {return 0;}
	}
	foreach(long x; OpApply()) {} // fails
		// test.d(10): Error: cannot uniquely infer foreach argument types
	
	// In contrast, it does work on plain arrays and ranges:
	
	int[] a;
	foreach(long x; a) {} // ok
	
	static struct Range {
		@property bool empty() {return false;}
		@property int front() {return 0;}
		void popFront() {}
	}
	foreach(long x; Range()) {} // ok
}
Comment 1 yebblies 2013-11-23 20:31:10 UTC
This case still doesn't work

void main() {

    static struct OpApply {
        int opApply(int delegate(int)) {return 0;}
    }
    foreach(long x; OpApply()) {} // fails
        // test.d(10): Error: cannot uniquely infer foreach argument types

}

The first was issue 9212
Comment 2 dlangBugzillaToGithub 2024-12-13 18:01:15 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/dmd/issues/18466

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB