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 · 2 comments
Open

Return "partial" OneOf types #181

juantxorena opened this issue Sep 26, 2024 · 2 comments

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

@Pxtl
Copy link

Pxtl commented Dec 5, 2024

Typecast-based implementation of same problem here:

#185

Works like this:

  OneOf<A, B, C> GetOneOfABC()
    => DoSomething();
    
  OneOf<A, B, C, D> GetOneOfABCD()
    => (SomeDPredicate()) 
    ? GetD()
    : GetOneOfABC(); //this is the cool part

where GetD() returns some D, and DoSomething() returns a OneOf<A, B, C>

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