Motive behind !empty() with front() instead of Optional front()

Per Nordlöw per.nordlow at gmail.com
Fri Mar 26 13:23:25 UTC 2021


On Friday, 26 March 2021 at 09:10:28 UTC, Per Nordlöw wrote:
> On Friday, 26 March 2021 at 08:49:30 UTC, Piotr Mitana wrote:
>> Actually changing front() to return optionals would probably 
>> break many things. However, it could be handy to add a method 
>> called nullableFront or frontOrNull (and nullableBack or 
>> backOrNull for bidirectional ranges) to std.range.
>
> Agreed. Alternative namings could be maybeFront or 
> optionalFront.

A D specific solution could be to add compiler diagnostics that 
checks whether a call to `x.front` and x.popFront() is guarded by 
an `if (!x.empty)`. Provided no mutation has happened since the 
call to !empty and `x` is not accessible via other threads.


More information about the Digitalmars-d mailing list