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
When using FluentResults with generic types like IReadOnlyList<T> it seems that implicit conversation doesn't work and I can't understand why. Maybe someone can enlighten me by this simple example.
What works without any problems is this kind of code:
privateasyncTask<string>FindMatchingItem(){await Task.Delay(1);return"1";}publicasyncTask<Result<string>>GetMatchingItem(){varitem=await FindMatchingItem();// Here we can simply return item, which will be automatically be converted into Result<string>returnitem;}
But, what doesn't work is this one here:
privateasyncTask<IReadOnlyList<string>>FindMatchingItems(){await Task.Delay(1);returnnewList<string>{"1","2","3"};}publicasyncTask<Result<IReadOnlyList<string>>>GetMatchingItems(){varitems=await FindMatchingItems();// Doesn't compile. Has to be: return Result.Ok(items);returnitems;}
The compiler complains with this error message: CS0266: Cannot implicitly convert type 'System.Collections.Generic.IReadOnlyList<string>' to 'FluentResults.Result<System.Collections.Generic.IReadOnlyList<string>>'. An explicit conversion exists (are you missing a cast?)
If I'm going to change the signature of FindMatchingItems() to return Task<List<string>> the error is gone:
// We changed the return type to be List instead of IReadOnlyListprivateasyncTask<List<string>>FindMatchingItems(){await Task.Delay(1);returnnewList<string>{"1","2","3"};}// But not here. It is still IReadOnlyListpublicasyncTask<Result<IReadOnlyList<string>>>GetMatchingItems(){varitems=await FindMatchingItems();returnitems;}
So can anyone explain, why List<string> can be converted into Result<IReadOnlyList<string>>, but the very same type IReadOnlyList<string> can't be converted into the desired Result type?
The text was updated successfully, but these errors were encountered:
When using FluentResults with generic types like
IReadOnlyList<T>
it seems that implicit conversation doesn't work and I can't understand why. Maybe someone can enlighten me by this simple example.What works without any problems is this kind of code:
But, what doesn't work is this one here:
The compiler complains with this error message:
CS0266: Cannot implicitly convert type 'System.Collections.Generic.IReadOnlyList<string>' to 'FluentResults.Result<System.Collections.Generic.IReadOnlyList<string>>'. An explicit conversion exists (are you missing a cast?)
If I'm going to change the signature of
FindMatchingItems()
to returnTask<List<string>>
the error is gone:So can anyone explain, why
List<string>
can be converted intoResult<IReadOnlyList<string>>
, but the very same typeIReadOnlyList<string>
can't be converted into the desired Result type?The text was updated successfully, but these errors were encountered: