Return explicitly "empty" MapResult (fail-fast)

zhade dragoonzombie at gmail.com
Thu Jan 1 19:20:25 UTC 2026


On Thursday, 1 January 2026 at 17:09:20 UTC, Paul Backus wrote:
>
> You could try something like this:
>
> ```d
> auto example_func(bool fail)
> {
>     string[] list;
>
>     if (fail)
>         list = [];
>     else
>         list = someExpensiveOperation();
>
>     return list
>         .filter!(...)
>         .map!(...);
> }
> ```
>
> Because `map` and `filter` are lazy, calling them on an empty 
> `list` won't do anything, but they will still return the 
> correct type.

Thank you, yes this will work but it eliminates the fail-fast. 
For readability I reduced the actual code to a simple example (in 
hindsight I should have kept it a bit more complex). I would need 
to wrap all the other logic that happens after 
"someExpensiveOperation" inside the 'else' block.


More information about the Digitalmars-d-learn mailing list