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
Unsure whether this is desirable behaviour, but I believe this implicit conversion can lead to bugs. I'm not sure where it provides value compared to the risk of misuse.
Consider the following contrived example:
using FluentResults;boolfetchUserName=false;booluseDefault=true;varres=fetchUserName? GetSomeUserName()// Result<string>:useDefault? Result.Ok()// oops, forgot to set the default. Implicit cast to Result<string>(default):new Error("No name found");
Console.WriteLine(res.IsSuccess ?$"Username is '{res.Value}'":"Nothing");Result<string>GetSomeUserName()=> Result.Ok("user");
My expectation is that res should have some valid string value. In this instance Result.Ok() is implicitly cast to Result<string>(default), and hides the fact that a string value was not assigned.
Whilst it might be obvious in this simple example, in more complex scenarios where .Bind() calls are chained it is much easier to miss. It feels like the compiler should catch such cases.
I would have expected the opposite conversion to work; Result<T> -> Result. Whilst this has the effect of losing T, the compiler would catch any expectations of T.
The text was updated successfully, but these errors were encountered:
I totally agree. We added in the last year some implicit conversions with the advantage that you don't have to type that much code. The disadvantage is (as you know) the missing type safety.
I'am also not a big fan of this implicit conversions and maybe I will remove it from the lib in the next major version.
Drizin
added a commit
to Drizin/FluentResults
that referenced
this issue
Aug 4, 2024
…aking some safety checks in the other implicit conversion that converts any object to a `Result<TValue>`:
- It should NOW wrap again something that is already IResultBase (or else a Fail result could be converted into a Success result)
- Now it can cast the result object to any compatible type (parent classes), not only object
- Will throw for nonsense conversions for safety
Refs altmann#171
Unsure whether this is desirable behaviour, but I believe this implicit conversion can lead to bugs. I'm not sure where it provides value compared to the risk of misuse.
Consider the following contrived example:
My expectation is that
res
should have some validstring
value. In this instanceResult.Ok()
is implicitly cast toResult<string>(default)
, and hides the fact that a string value was not assigned.Whilst it might be obvious in this simple example, in more complex scenarios where
.Bind()
calls are chained it is much easier to miss. It feels like the compiler should catch such cases.I would have expected the opposite conversion to work;
Result<T> -> Result
. Whilst this has the effect of losingT
, the compiler would catch any expectations ofT
.The text was updated successfully, but these errors were encountered: