Return explicitly "empty" MapResult (fail-fast)

Dukc ajieskola at gmail.com
Fri Jan 30 16:10:21 UTC 2026


On Thursday, 1 January 2026 at 17:01:25 UTC, zhade wrote:
> Hi, I am trying to fail-fast and need to return "something" 
> that works together with MapResult.
>
> I came up with a solution but wanted to know if its the 
> supposed way to do it.
>
> What I learned is that the lambdas of `map` and `filter` are 
> part of the return type.

`MapResult` does not have an universal empty value. If you want 
its result to be empty, you need to give it a source range that 
is also empty. Fortunately, in your case it is easy to do:

```d
auto example_func(bool fail)
{
     auto list = fail? []: someExpensiveOperation();

     return list
         .filter!(a => a != "expensive")
         .map!(a => tuple!("value", "numLetters")(a, a.length));
}
```

You could also iterate the result range until it's empty before 
returning it, but that's wasting CPU cycles.


More information about the Digitalmars-d-learn mailing list