C
C#3mo 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);
}
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?
5 Replies
pseudo
pseudoOP3mo ago
@pride century 젠젠 Sorry for ping but im actually still confused :TERROR: I get what you were saying about the .Value stuff
public class ParseResult<TResult, TError>
{
private readonly TResult? _result;
private readonly TError? _error;

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

public bool IsError() => _error is not null;

public TResult Result()
{
if (_result is null)
throw new Exception("Given result type is an error");

return (TResult) _result;
}

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

public static ParseResult<TResult, TError> CreateError(TError error)
=> new(default, error);
}
public class ParseResult<TResult, TError>
{
private readonly TResult? _result;
private readonly TError? _error;

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

public bool IsError() => _error is not null;

public TResult Result()
{
if (_result is null)
throw new Exception("Given result type is an error");

return (TResult) _result;
}

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

public static ParseResult<TResult, TError> CreateError(TError error)
=> new(default, error);
}
But how does this build fine with unconstrained type
public TResult Result()
{
if (_result is null)
throw new Exception("Given result type is an error");

return (TResult) _result;
}
public TResult Result()
{
if (_result is null)
throw new Exception("Given result type is an error");

return (TResult) _result;
}
I.e. this would crash surely for int?
alex
alex3mo ago
i dont know what you mean by boxing here
pseudo
pseudoOP3mo ago
sorry term for another lang Nullable<int> is boxed int altho is actually wrong if Nullable<T> is also a value-type, i just meant a nullable value type with it
alex
alex3mo ago
thats just a nullable value type and honestly dont put too much thought into it thats just how it works just make the fields unnullable and cope with it
pseudo
pseudoOP3mo ago
:TERROR: ty anyways
Want results from more Discord servers?
Add your server