Return explicitly "empty" MapResult (fail-fast)

Dukc ajieskola at gmail.com
Fri Jan 30 16:17:34 UTC 2026


On Thursday, 1 January 2026 at 19:20:25 UTC, zhade wrote:
> 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.

It doesn't. In case of `fail`, `filter` and `map` have nothing to 
process and therefore they stop immediately. It is just as 
fail-fast as `return fail? []: someExpensiveOperation()`.


More information about the Digitalmars-d-learn mailing list