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

Meta jared771 at gmail.com
Wed Mar 24 19:32:22 UTC 2021


On Wednesday, 24 March 2021 at 19:23:21 UTC, Per Nordlöw wrote:
> What's the motive behinds D's range design choice of needing
>
>     if (!empty)
>     {
>         // use front or back
>     }
>
> instead of having front returning an optional/maybe type with 
> enforced pattern matching?
>
> Lack of builtin Optional type?
>
> Choosing the Optional path would have avoided the need for 
> putting error diagnostics such as
>
> https://github.com/dlang/phobos/commit/9bd2f2ba8ff1124a044560c4e6912a13cb5ac694
>
> in the standard library of such an alternative solution.

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).


More information about the Digitalmars-d mailing list