You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Covariance is only working with interfaces. Meaning something like this is possible:
public interface IVehicle { }
public class Car : IVehicle { }
public class Bus : IVehicle { }
IResult<IVehicle> carResult1 = Result.Success(new Car());
The solution seams simple, just go with IResult rather than Result.
But all the extension methods (those) are on Result, and not on IResult.
Is there a possibility to put all extension methods onto IResult rather than Result?
The "creation" methods (those) can stay on Result, but they return an IResult.
I guess when the extension methods are moved to IResult, we break compatibility...
If the extension methods are copied to IResult, the codebase gets much bigger...
The text was updated successfully, but these errors were encountered:
redx177
changed the title
Moving extension methods on IResult (to enable covariance)
Moving extension methods onto IResult (to enable covariance)
Feb 21, 2024
Yes, it's one of the C# limitations. The solution here will be to specify type explicitely like
var carResult2 = Result.Success<IVehicle>(new Car());
The problem I see with the extensions to IResult are
Async extensions. Tasks/ValueTasks are not delegates and interfaces, so Task<Result<int>> with not be suitable for extensions accepting Task<IResult<T>>, only to extensions accepting Task<Result<T>>. The possible solution is to parameterize extensions to accept Task<TResult<TValue>> where TResult : IResult<TValue>, but C# will not infer type parameters, so everyone will have to provide them explicitely
IResult is an interface, so there will be a lot of unnecessary allocations
Covariance is only working with interfaces. Meaning something like this is possible:
This on the other hand is not possible:
The solution seams simple, just go with
IResult
rather thanResult
.But all the extension methods (those) are on
Result
, and not onIResult
.Is there a possibility to put all extension methods onto
IResult
rather thanResult
?The "creation" methods (those) can stay on
Result
, but they return anIResult
.I guess when the extension methods are moved to
IResult
, we break compatibility...If the extension methods are copied to
IResult
, the codebase gets much bigger...The text was updated successfully, but these errors were encountered: