Closed olivermue closed 3 weeks ago
If someone needs a simple workaround, return Result.Ok(items)
.
TLDR; You can also use items.ToList()
to make this work. It seems to be a limitation of the C# spec.
I was messing around with this today and this same thing happens if you use IEnumerable
instead of IReadOnly*
variants as well. I have a feeling this is related to this SO answer and this SO answer by Eric Lippert where he talks about implicit conversion of interface types. The spec doesn't support this and there aren't plans to add it. What does work currently is considered a bug that they are leaving in place to not break existing production systems.
Because the result is wrapped in Task
, it sort of obscures the defined implicit conversion that would work if it was a synchronous call. You can reproduce that same issue because Task<IEnumerable<string>> MyMethod() => Task.FromResult(["A"]);
won't compile for the same reason. We know []
is an IEnumerable
, but because it is wrapped in Task
, it cannot implicitly convert to that type and you either need to return Task.FromResult<IEnumerable<string>>(["A"])
or await
the task.
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?