mcintyre321/OneOf

Return "partial" OneOf types

Opened this issue · 1 comments

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 commented

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