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

Return "partial" OneOf types #181

Open
juantxorena opened this issue Sep 26, 2024 · 1 comment
Open

Return "partial" OneOf types #181

juantxorena opened this issue Sep 26, 2024 · 1 comment

Comments

@juantxorena
Copy link

I'm liking this library, but I've found a situation which IMHO is a bit annoying and doesn't really make sense. Consider this code:

public async Task<OneOf<None, Unknown, False>> DoThings(Object input)
{
    if (something)
    {
        return new False();
    }
    OneOf<None, Unknown> result = await OtherMethod(input);
    return result;
}

It fails to compile, because it cannot convert from OneOf<None, Unknown> to OneOf<None, Unknown, False>. It makes sense, but considering that I'm returning a False, or a None or Unknown, I think it should work. Instead, I have to manually match the output and return the types, like this, which is pretty redundant:

public async Task<OneOf<None, Unknown, False>> DoThings(Object input)
{
    if (something)
    {
        return new False();
    }
    OneOf<None, Unknown> result = await OtherMethod(input);
    return result.Match<OneOf<None, Unknown, False>>(
	none => new None(),
	unknown => new Unknown()
    );
}

Maybe it's difficult to implement, but IMHO it makes sense that it should work.

@Pxtl
Copy link

Pxtl commented Oct 23, 2024

I have a PR up that provides a .WithType() function that lets you bolt on the missing types to an existing OneOf result object. It makes your case less verbose.

public async Task<OneOf<None, Unknown, False>> DoThings(Object input)
{
    if (something)
    {
        return new False();
    }
    OneOf<None, Unknown> result = await OtherMethod(input);
    return result.WithType<False>();
}

It still means your types must be in the same order and just lets you bolt on one more type at the end, but it's something.

add new method WithType to add types to a OneOf #171

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