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

Steven Schveighoffer schveiguy at gmail.com
Sat Mar 27 01:06:37 UTC 2021


On 3/26/21 3:20 PM, Nick Treleaven wrote:
> On Friday, 26 March 2021 at 15:53:39 UTC, Steven Schveighoffer wrote:
>> Notice that it's an assert, which means it can be removed in 
>> correctly-written programs. If you return an Optional, emptiness MUST 
>> be checked on every access to an element, even when you know (or have 
>> proven) it's not empty.
> 
> An optional would have an `unwrap` method for this purpose, which also 
> only asserts it is not empty and returns a value.

That could be possible if the Optional type lazily fetches the 
value/empty status, in which case now you are dealing with delegates 
and/or closures. I don't think this is the right path.

-Steve


More information about the Digitalmars-d mailing list