C#C
C#15mo ago
pseudo

Any case where `TError` can't be boxed in example?

So I have the following:
public class ParseResult<TResult, TError>
{
    private readonly TResult? _result;
    private readonly TError? _error;

    private ParseResult(TResult? result, TError? error)
    {
        _result = result;
        _error = error;
    }

    public static ParseResult<TResult, TError> CreatedResult(TResult result)
        => new ParseResult<TResult, TError>(result, null);
}

But am getting an error on last line passing
null
in, I can see it resolves if i add a class constraint to the generic arg, or if i give default, but confused why it cant infer that TError? is always gonna be boxed, i.e. if i had
int
as TError surely it can work out Nullable<int> from TError?
Was this page helpful?