Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EitherAsync returns Null for Left value when used in Linq query #1192

Open
naveen-negi opened this issue Feb 16, 2023 · 1 comment
Open

EitherAsync returns Null for Left value when used in Linq query #1192

naveen-negi opened this issue Feb 16, 2023 · 1 comment

Comments

@naveen-negi
Copy link

naveen-negi commented Feb 16, 2023

When I'm trying to chain EitherAsync, final value is always null in case of left. I have a sample below. Function HappyPath works as expected, however, EarlyExitOnFailure return null instead of first that it encounters in the chain.

public static class TestEitherAsync
{
    public static EitherAsync<A, B> EitherAOrBLeft()
    {
        return new A();
    }

    public static EitherAsync<A, B> EitherAOrBRight()
    {
        return new B();
    }

    public static EitherAsync<C, D> EitherCOrDLeft()
    {
        return new C();
    }

    public static EitherAsync<C, D> EitherCOrDRight()
    {
        return new D();
    }

    public static async Task<E> EarlyExitOnFailure()
    {
        var result = await (from b in EitherAOrBLeft()
            from d in EitherCOrDLeft()
            select new E(b, d)).FirstOrDefaultAsync();
        return result!;
    }

    public static async Task<E> HappyPath()
    {
        var result = await (from b in EitherAOrBRight()
            from d in EitherCOrDRight()
            select new E(b, d)).FirstOrDefaultAsync();
        return result!;
    }
}

public record A();
public record B();
public record C();

public record D();

public record E(B First, D Second);
@louthy
Copy link
Owner

louthy commented Feb 16, 2023

It’s hardly surprising, calling FirstOrDefault for empty sequences returns null. It’s a LINQ operator that I don’t control.

Use Match to work with the Left or Right values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants