On Wed, Dec 13, 2017 at 03:33:51PM -0500, Steven Schveighoffer via 
Digitalmars-d wrote:
> On 12/13/17 2:33 PM, Jonathan M Davis wrote:
[...]
> The best way I think to have ranges work is to only modify them in
> popFront/popBack, and the ctor.
[...]

I don't see anything wrong with doing work in .empty or .front, as long
as the overall result looks the same.  Sometimes, if .front is expensive
to compute, you may want to defer the work until it's actually needed.
For this, a caching implementation of .front might work best, though at
the cost of slightly more complexity:

        struct MyRange {
                private WorkParams params;
                private Nullable!T frontValue;

                @property bool empty() { ... }
                @property T front() {
                        if (frontValue.isNull)
                        {
                                frontValue = doExpensiveWork(params);
                        }
                        return frontValue;
                }
                void popFront() {
                        params = setupNextItemParams();
                }
        }

The use of Nullable here is just for illustration, of course. In an
actual implementation you can probably find cheaper ways of doing the
same thing.


T

-- 
My program has no bugs! Only undocumented features...

Reply via email to