Ranges: is it ok if front is a data member?
Joseph Rushton Wakeling
joseph.wakeling at webdrake.net
Fri Dec 13 07:20:18 PST 2013
On 13/12/13 16:52, Marco Leise wrote:
> Most non-trivial ranges do the actual work in `popFront()' and
> return a cached value from `front'. It has been argued as a
> design quirk, that this in general leads to:
>
> struct Range
> {
> bool popFrontHasBeenCalledOnce = false;
> T current;
>
> @property T front()
> {
> if (!popFrontHasBeenCalledOnce)
> {
> popFront(); // initializes `current'
> }
> return current;
> }
>
> […]
> }
For example in much of std.random. With classes you can get round it by
defining a default constructor, but with structs it can create some tricky
situations.
I have wondered about the feasibility of a method called something like .first()
which would basically be called the very first time one calls _any_ method of
the struct/class in question, and would perform the appropriate initialization.
More information about the Digitalmars-d-learn
mailing list