In general, who should do more work: popFront or front?

ag0aep6g anonymous at example.com
Tue Jun 15 05:40:44 UTC 2021


On 15.06.21 07:17, mw wrote:
> 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.

`const` and `inout` mean the same thing here: Both functions cannot 
modify the elements of the array.

The difference lies in `ref` alone. `front` receives a copy, so it can't 
change the length of the array you pass in. `popFront` receives by 
reference, so it can (and does).


More information about the Digitalmars-d-learn mailing list