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

Returning OneOf<IEnumerable<T>, Error> #186

Open
brumlemann opened this issue Nov 11, 2024 · 1 comment
Open

Returning OneOf<IEnumerable<T>, Error> #186

brumlemann opened this issue Nov 11, 2024 · 1 comment

Comments

@brumlemann
Copy link

Hi,

Only started using OneOf last week on a refurbishing project for one of our old .Net FW projects moving it to .Net 8. It worked great for removing the "running on bugs" anti pattern. No more magical error handler in the pipeline outside of the specific IActionResult action logic.

However, now I need to tackle the next problem, a very inefficient bulk operation and I would like the following signature on my new bulk db method

public OneOf<IEnumerable, Error>

with yield return for each actual data point and return of error if something goes wrong. Heck even

public OneOf<IEnumerable, IEnumerable>

would make sense if you're willing to capture all the errors but proceed with the processing of the input. Maybe it even makes more sense in a way.

However, the compiler says no :-(

The body of 'BulkInsert' cannot be an iterator block because 'OneOf<System. Collections. Generic. IEnumerable,Error>' is not an iterator interface type (actual types obfuscated).

Any chance of something like this or is this just outside the paradigm? The more I look at it the more it feels like it, but there might be smarter people than me out there ;-)

Regards
M

@emperador-ming
Copy link

Hi, @brumlemann ,

This is a skeleton that approaches the issue using exceptions. Make sure you use .NET 9, which has introduced significant performance improvements in this regard.

using OneOf;

static IEnumerable<int> GetIterator()
{
    for (int i = 0; i < 10; i++)
    {
        if(i == 5)
        {
            throw new Exception();
        }
        
        yield return i++;
    }
}

static OneOf<List<int>, Error> Process()
{
    try
    {
        return GetIterator().ToList();
    }
    catch (Exception)
    {
        return new Error();
    }
}

record Error();

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