Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove implicit Result -> Result<T> #171

Open
Tim-Hodge opened this issue Jan 13, 2023 · 2 comments
Open

Remove implicit Result -> Result<T> #171

Tim-Hodge opened this issue Jan 13, 2023 · 2 comments

Comments

@Tim-Hodge
Copy link

Tim-Hodge commented Jan 13, 2023

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;

bool fetchUserName = false;
bool useDefault = true;

var res = 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.

@altmann
Copy link
Owner

altmann commented Feb 4, 2023

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
@anthonylevine
Copy link

Would love to see this pulled out as well. Thanks again for the great library!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants