In general, who should do more work: popFront or front?
mw
mingwu at gmail.com
Tue Jun 15 05:17:06 UTC 2021
On Tuesday, 15 June 2021 at 05:03:45 UTC, surlymoor wrote:
> On Tuesday, 15 June 2021 at 04:57:45 UTC, mw wrote:
>> In English, `front` is a noun, `popFront` have a verb in it,
>> so you know who should do more work :-)
>
> Sure, but, for example, the front method of Phobos' MapResult
> is the one applying the predicate to the source's front.
> There's a clear separation of responsibilities between popFront
> and front: the former iterates over the source, and the latter
> performs an operation that produces the range's elements.
I think there is another convention (although it's not formally
enforced, but should be) is:
-- `obj.front() [should be] const`, i.e. it shouldn't modify
`obj`, so can be called multiple times at any given state, and
produce the same result
-- `obj.popFront()` perform the actual mutation of the internal
state the `obj`, each invocation will change the object's state
once.
e.g.
https://dlang.org/library/std/range/primitives/front.html
the 2nd decl:
dchar front(T) (
scope const(T)[] a
) pure @property @safe
if (isAutodecodableString!(T[]));
you can see `const`
but
https://dlang.org/library/std/range/primitives/pop_front.html
void popFront(C) (
scope ref inout(C)[] str
) pure nothrow @trusted
if (isAutodecodableString!(C[]));
it's `ref inout`, which means the passed-in object is intended to
be modified inside the method in-place.
More information about the Digitalmars-d-learn
mailing list