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

ResultOrErrors deconstruct #217

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Drizin
Copy link

@Drizin Drizin commented Aug 3, 2024

Currently all deconstructors return a pair bool isSuccess/bool isFailed, which is slightly redundant (IMO only one would be enough, and if we are just interested in a single bool result we can just get the prop instead of using a desconstructor).

It becomes even more redundant in Result<TValue> since in that case we're also (we should be!) returning TValue value and possibly List<IError> errors - success/failure can just be infered from those values (if they are null) instead of having 4 different methods for testing the same thing.

Previously I've been just discarding the redundant variables: var (_, _, result, errors) = MyServiceCall(),
but I thought that this cleaner/concise syntax could benefit everyone - so the 2-tuple deconstruct would just return us TValue value and List<IError> errors. (This concise style is used in golang and becoming popular elsewhere):
var (result, errors) = MyServiceCall().

This PR adds to Result that new golang-style deconstruct: Deconstruct(out TValue value, out List<IError> errors).

ResultBase still has Deconstruct(out bool isSuccess, out bool isFailed), so non-generic Results (without TValue) can still use that isSuccess/isFailed deconstruction (which IMO is redundant and provide little value).

Breaking change: if someone uses Result<TValue> and do the 2-tuple deconstruction (which is unlikely since it would ignore the possible return value and also the error list) then this deconstruction will break (will get different types and most likely will break compilation).

The <bool,bool> deconstructor (which I think should be removed) was moved down from ResultBase to Result. So that 2-tuple deconstruct is still available when there's no underlying TValue (people using Result<TValue> should never be using that construct in the first place since it ignores the important TValue).

Drizin added a commit to Drizin/FluentResults that referenced this pull request Aug 3, 2024
- I think it doesn't make sense to deconstruct both isSuccess/isFailure (just noisy, one would be enough and promoves uniform usage patterns)
- If we're just interested in that specific property (success or failure, without getting the possible Result or IErrors) then we can just get that property
- So if you're using a destructor on `Result<TValue>` you NEED to get TValue and optionally List<IError>.
- For the non-generic Result (the one that does NOT hold an underlying TValue) the destructor `<bool isSuccess, List<IError> errors>` was moved down from the base class

Refs altmann#217
Result<TValue> now has a new deconstruct (golang-style style): `Deconstruct(out TValue value, out List<IError> errors)`
Returning isSuccess and isFailed were redundant since we can infer success/failure from non-null values in `TValue value` and `List<IError> errors`.
Breaking change: anyone who was using Result<TValue> and was using the descontruct that returns bool/bool  (that person was already ignoring the TValue!) will probably get a build-break since the returned tuple won't be bool anymore

`Deconstruct(out bool isSuccess, out bool isFailed)` was moved from ResultBase to the Results class (the one without TValue) so it can still use that isSuccess/isFailed deconstruction.
@Drizin Drizin force-pushed the feature-resultOrErrors-deconstruct branch from a07f12e to 5b300e6 Compare August 3, 2024 22:13
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

Successfully merging this pull request may close these issues.

1 participant