Return explicitly "empty" MapResult (fail-fast)

zhade dragoonzombie at gmail.com
Thu Jan 1 19:30:11 UTC 2026


On Thursday, 1 January 2026 at 19:06:44 UTC, monkyyy wrote:
> On Thursday, 1 January 2026 at 17:01:25 UTC, zhade wrote:
>> 
>> Is this the supposed way to do it?
>
> The api was not written to be introspective and d's type theory 
> has holes in it
>
> Given a clean slate and modern knowledge and a design goal of 
> allowing introsection I would expect `return 
> typeof(return).init;` to work. Youd have to be writting your 
> own algorthims lib tho.
>
> If your lamda was an alias and you knew the range was empty on 
> init you should be able to get this sort of thing working:
>
> ```d
> alias F=...;
> if(cond1){
>   return r.map!F;
> } else {
>   return typeof(r).init.map!F;
> }
> ```
>
> but your off the garden path and your likely learn some new 
> compiler bugs :D

Thank you, I tried this (not quite as a lib but inlined) and it 
works. If I were to extract it further I believe I would still 
ultimately come to the solution I currently have which is a 
separate function for the map and filtering.

```d

auto example_func(bool fail)
{
     alias filterFunc = a => a != "expensive";
     alias mapFunc = a => tuple!("value", "numLetters")(a, 
a.length);

     if (fail)
     {
         return string[].init
             .filter!filterFunc
             .map!mapFunc;
     }

     auto list = someExpensiveOperation();

     return list
         .filter!filterFunc
         .map!mapFunc;
}
```



More information about the Digitalmars-d-learn mailing list