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
Comment #2 by k.hara.pg — 2013-10-14T20:39:43Z
Comment #3 by k.hara.pg — 2013-10-14T20:44:15Z
(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
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/800631a95964ff15a4dadb15b031f80222449031 fix Issue 11261 - Can't infer types without explicit slice in foreach https://github.com/D-Programming-Language/dmd/commit/339deaa9be455c98aa00c4f0741c76b8774b0e41 Merge pull request #2668 from 9rnsr/fix11261 [REG2.064a] Issue 11261 - Can't infer types without explicit slice in foreach
Comment #5 by github-bugzilla — 2013-10-15T00:59:36Z
Commit pushed to 2.064 at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/718dfbb02f8ca7356e8f46637b2e90e2f81d96d0 Merge pull request #2668 from 9rnsr/fix11261 [REG2.064a] Issue 11261 - Can't infer types without explicit slice in foreach