Return explicitly "empty" MapResult (fail-fast)

kdevel kdevel at vogtner.de
Sun Feb 1 09:02:51 UTC 2026


On Sunday, 1 February 2026 at 03:38:34 UTC, Ali Çehreli wrote:
> On 1/31/26 7:36 AM, kdevel wrote:
>
> >> >        auto list = fail
> >> >           ? typeof(someExpensiveOperation()).init
> >> >           : someExpensiveOperation();
>
> That works because in this simple code, which was to-me-clearly 
> was an example to show the issue, someExpensiveOperation() does 
> not take any parameters. That's why you can write that code in 
> that "less complex" way.

Parameters to someExpensiveOperation do not make a difference:

    auto someExpensiveOperation(int dummy) {
       return [ "hello" ];
    }

    auto example_func (bool fail)
    {
       int n = 3;

       auto list = fail
          ? someExpensiveOperation(n).init
          : someExpensiveOperation(n);

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


More information about the Digitalmars-d-learn mailing list