Jump to page: 1 2 3
Thread overview
User defined type and foreach
Nov 16, 2017
Tony
Nov 16, 2017
Andrea Fontana
Nov 16, 2017
Tony
Nov 16, 2017
Jonathan M Davis
Nov 16, 2017
Tony
Nov 16, 2017
ag0aep6g
Nov 16, 2017
Tony
Nov 16, 2017
Tony
Nov 17, 2017
Tony
Nov 17, 2017
H. S. Teoh
Nov 17, 2017
Tony
Nov 17, 2017
Mike Parker
Nov 17, 2017
Jonathan M Davis
Nov 17, 2017
Tony
Nov 17, 2017
Jonathan M Davis
Nov 18, 2017
Tony
Nov 18, 2017
Tony
Nov 18, 2017
Jonathan M Davis
Nov 18, 2017
H. S. Teoh
November 16, 2017
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
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
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
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
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
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
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
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
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
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
« First   ‹ Prev
1 2 3