Bug 11261 – Can't infer types without explicit slice in foreach
Status
RESOLVED
Resolution
FIXED
Severity
regression
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-10-14T10:54:00Z
Last change time
2013-10-15T01:14:36Z
Keywords
pull, rejects-valid
Assigned to
nobody
Creator
Jesse.K.Phillips+D
Comments
Comment #0 by Jesse.K.Phillips+D — 2013-10-14T10:54:11Z
When using a range to proved a key-value tuple, the compiler is unable to infer the key-value in a foreach when slice is not present (2). Using an array continues to work (1). This functioned in 2.63.
import std.typecons;
import std.range;
class Container {
alias Tuple!(string, "key", string, "value") Key;
InputRange!Key opSlice() {
// 1 Key[] opSlice() {
Range r;
return inputRangeObject(r);
// 1 return [Key("myKey", "My Value")];
}
private struct Range {
enum empty = false;
auto popFront() {
}
auto front() {
return Key("myKey", "myValue");
}
}
}
void main() {
auto container = new Container();
foreach(k,v; container)
// 2 foreach(k,v; container[])
{ break; }
}
Comment #1 by Jesse.K.Phillips+D — 2013-10-14T11:36:46Z
While the version with an explicit slice does infer types, it is inferring the wrong ones.
k = uint
v = Tuple!(string, "key", string, "value")
This is consistent with 2.63, without the explicit slice the types are
k = string
v = string
(In reply to comment #1)
> While the version with an explicit slice does infer types, it is inferring the
> wrong ones.
>
> k = uint
> v = Tuple!(string, "key", string, "value")
>
> This is consistent with 2.63, without the explicit slice the types are
>
> k = string
> v = string
In the first case (foreach (k, v; container)), both k and v should be inferred to string.
However in the second case (foreach (k, v; container[])) you _cannot_ get same inference result. Because std.range.InputRange interface defines an opApply with indexing (size_t + KeyType), and it is preferentially used for foreach argument type deduction.
Comment #4 by github-bugzilla — 2013-10-15T00:58:43Z