On 1/1/26 9:01 AM, zhade wrote:
Hi, I am trying to fail-fast and need to return "something" that works together with MapResult.

A method I used in the past is to put the entire range-generating code inside a function that has a default argument (makeRange below):

import std;

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

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

    auto makeRange(R)(R r = typeof(someExpensiveOperation()).init) {
        return r
            .filter!filterFunc
            .map!mapFunc;
    }

    if (fail)
    {
        return makeRange();
    }

    auto list = someExpensiveOperation();

    return makeRange(list);
}

void main() {
    example_func(true);
}

makeRange need not be a template but the code is a little cleaner when it is.

That solution was a part of the following presentation (step 98/121 there):

  https://youtu.be/dRORNQIB2wA?t=2628

Ali

Reply via email to