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

IGotD- nise at nise.com
Wed Mar 24 21:09:52 UTC 2021


On Wednesday, 24 March 2021 at 19:32:22 UTC, Meta wrote:
>
> Most/all of the range stuff was Andrei's idea, and his C++ 
> background heavily informed the design. Returning Optional<T> 
> to represent that a calculation might fail is not common in 
> C++, or at least wasn't back then. In general, more 
> functional-oriented techniques have only recently started 
> diffusing into the C++ culture (or at least it feels that way 
> to me; I don't really follow C++ closely anymore).

That it is because of historical reasons makes sense. From a 
programming point of view, it doesn't really matter since you 
must check your Optional<T> return value anyway and you still get 
a check regardless. Performance wise it doesn't matter.

Another motive would be that it would force to check if the 
return value is not empty, however you might not want that. For 
example in a loop and you know the amount of elements in advance 
then the check is unnecessary.


More information about the Digitalmars-d mailing list