Thread overview | |||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
November 16, 2017 User defined type and foreach | ||||
---|---|---|---|---|
| ||||
I made a stack data type and created an opIndex() so it could be turned into a dynamic array, and created empty() (unfortunate name), front() and popFront() methods, which I read allow it to be used with foreach. However, when I use the class with foreach, the opindex gets called to create a dynamic array, rather than use the empty(),front(),popFront() routines. I would prefer it use the three methods, rather than create a dynamic array. |
November 16, 2017 Re: User defined type and foreach | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tony | On Thursday, 16 November 2017 at 08:03:48 UTC, Tony wrote: > I made a stack data type and created an opIndex() so it could be turned into a dynamic array, and created empty() (unfortunate name), front() and popFront() methods, which I read allow it to be used with foreach. > > However, when I use the class with foreach, the opindex gets called to create a dynamic array, rather than use the empty(),front(),popFront() routines. I would prefer it use the three methods, rather than create a dynamic array. You can try to implement opApply(). Check: http://ddili.org/ders/d.en/foreach_opapply.html |
November 16, 2017 Re: User defined type and foreach | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrea Fontana | On Thursday, 16 November 2017 at 08:26:25 UTC, Andrea Fontana wrote:
> On Thursday, 16 November 2017 at 08:03:48 UTC, Tony wrote:
>> I made a stack data type and created an opIndex() so it could be turned into a dynamic array, and created empty() (unfortunate name), front() and popFront() methods, which I read allow it to be used with foreach.
>>
>> However, when I use the class with foreach, the opindex gets called to create a dynamic array, rather than use the empty(),front(),popFront() routines. I would prefer it use the three methods, rather than create a dynamic array.
>
> You can try to implement opApply().
> Check:
> http://ddili.org/ders/d.en/foreach_opapply.html
Thanks. Interesting that that page does not mention the behavior I am seeing, which is that foreach over a user-defined datatype can be implemented with only a 'T[] opIndex()' method.
|
November 16, 2017 Re: User defined type and foreach | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tony | On Thursday, November 16, 2017 08:43:17 Tony via Digitalmars-d-learn wrote:
> On Thursday, 16 November 2017 at 08:26:25 UTC, Andrea Fontana
>
> wrote:
> > On Thursday, 16 November 2017 at 08:03:48 UTC, Tony wrote:
> >> I made a stack data type and created an opIndex() so it could
> >> be turned into a dynamic array, and created empty()
> >> (unfortunate name), front() and popFront() methods, which I
> >> read allow it to be used with foreach.
> >>
> >> However, when I use the class with foreach, the opindex gets
> >> called to create a dynamic array, rather than use the
> >> empty(),front(),popFront() routines. I would prefer it use the
> >> three methods, rather than create a dynamic array.
> >
> > You can try to implement opApply().
> > Check:
> > http://ddili.org/ders/d.en/foreach_opapply.html
>
> Thanks. Interesting that that page does not mention the behavior I am seeing, which is that foreach over a user-defined datatype can be implemented with only a 'T[] opIndex()' method.
What you're seeing is intended for containers. If you implement opIndex or opSlice with no parameters so that you can slice the object with no indices, the idea is that it's going to return a range. foreach knows about this so that you can then iterate over a container with foreach rather than having to explicitly call a function on the container to get the range to then iterate over. e.g. with
RedBlackTree rbt;
...
you can do
foreach(e; rbt)
{...}
instead of
foreach(e; rbt[])
{...}
though if you then pass the container to a range-based function, you're still going to have to explicitly slice it.
- Jonathan M Davis
|
November 16, 2017 Re: User defined type and foreach | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tony | On 11/16/17 3:03 AM, Tony wrote:
> I made a stack data type and created an opIndex() so it could be turned into a dynamic array, and created empty() (unfortunate name), front() and popFront() methods, which I read allow it to be used with foreach.
>
> However, when I use the class with foreach, the opindex gets called to create a dynamic array, rather than use the empty(),front(),popFront() routines. I would prefer it use the three methods, rather than create a dynamic array.
Remove the opIndex, and see if it works. If it does, then this is a bug. The compiler should try to use the type as a range before seeing if opIndex gives it something.
-Steve
|
November 16, 2017 Re: User defined type and foreach | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tony | On 11/16/2017 09:03 AM, Tony wrote: > However, when I use the class with foreach, the opindex gets called to create a dynamic array, rather than use the empty(),front(),popFront() routines. I would prefer it use the three methods, rather than create a dynamic array. https://issues.dlang.org/show_bug.cgi?id=14619 |
November 16, 2017 Re: User defined type and foreach | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Thursday, 16 November 2017 at 12:56:18 UTC, Steven Schveighoffer wrote:
> On 11/16/17 3:03 AM, Tony wrote:
>> I made a stack data type and created an opIndex() so it could be turned into a dynamic array, and created empty() (unfortunate name), front() and popFront() methods, which I read allow it to be used with foreach.
>>
>> However, when I use the class with foreach, the opindex gets called to create a dynamic array, rather than use the empty(),front(),popFront() routines. I would prefer it use the three methods, rather than create a dynamic array.
>
> Remove the opIndex, and see if it works. If it does, then this is a bug. The compiler should try to use the type as a range before seeing if opIndex gives it something.
>
Yes, if I remove opIndex() from the class, doing a foreach loop causes popFront(),empty(), and front() to be called and the foreach loop works.
|
November 16, 2017 Re: User defined type and foreach | ||||
---|---|---|---|---|
| ||||
Posted in reply to ag0aep6g | On Thursday, 16 November 2017 at 13:10:11 UTC, ag0aep6g wrote: > On 11/16/2017 09:03 AM, Tony wrote: >> However, when I use the class with foreach, the opindex gets called to create a dynamic array, rather than use the empty(),front(),popFront() routines. I would prefer it use the three methods, rather than create a dynamic array. > > https://issues.dlang.org/show_bug.cgi?id=14619 And also this one which was marked as a duplicate of yours https://issues.dlang.org/show_bug.cgi?id=16374 "When lowering a foreach, the compiler gives priority to opSlice over front/popFront/empty, which is counter-intuitive (and also undocumented)." "opSlice" -> "opSlice or opIndex". |
November 16, 2017 Re: User defined type and foreach | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tony | On Thursday, 16 November 2017 at 13:35:13 UTC, Tony wrote:
> On Thursday, 16 November 2017 at 13:10:11 UTC, ag0aep6g wrote:
>> On 11/16/2017 09:03 AM, Tony wrote:
>>> However, when I use the class with foreach, the opindex gets called to create a dynamic array, rather than use the empty(),front(),popFront() routines. I would prefer it use the three methods, rather than create a dynamic array.
>>
>> https://issues.dlang.org/show_bug.cgi?id=14619
>
> And also this one which was marked as a duplicate of yours
>
> https://issues.dlang.org/show_bug.cgi?id=16374
>
> "When lowering a foreach, the compiler gives priority to opSlice over front/popFront/empty, which is counter-intuitive (and also undocumented)."
>
> "opSlice" -> "opSlice or opIndex".
That should be "opSlice or opIndex-with-no-parameters"
|
November 16, 2017 Re: User defined type and foreach | ||||
---|---|---|---|---|
| ||||
Posted in reply to ag0aep6g | On 11/16/17 8:10 AM, ag0aep6g wrote:
> On 11/16/2017 09:03 AM, Tony wrote:
>> However, when I use the class with foreach, the opindex gets called to create a dynamic array, rather than use the empty(),front(),popFront() routines. I would prefer it use the three methods, rather than create a dynamic array.
>
> https://issues.dlang.org/show_bug.cgi?id=14619
I took a shot at fixing. Way more complex than I realized.
-Steve
|
Copyright © 1999-2021 by the D Language Foundation