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

Either: How to indicate success/failure from a void method? #853

Closed
MrYossu opened this issue Feb 8, 2021 · 6 comments
Closed

Either: How to indicate success/failure from a void method? #853

MrYossu opened this issue Feb 8, 2021 · 6 comments

Comments

@MrYossu
Copy link
Contributor

MrYossu commented Feb 8, 2021

Simple question. Suppose I have a method that does not return data, for example...

public async Task SaveCustomer(Customer customer) {
  // Save customer to the database
}

Now I want the return type to indicate if the operation succeeded. The obvious choice seems to be an Either, but that has two generic types, one usually used for the returned data, and one for the data. However, as I don't have any data to return, it seems pointless having a generic type that I won't use.

I don't think Try would be the right thing, as that assumes a return type for the happy path, whereas I don't have one. I only want to return something (an error message) if the operation failed.

Anyone able to advise? Thanks

@TysonMN
Copy link
Contributor

TysonMN commented Feb 8, 2021

I don't think Try would be the right thing, as that assumes a return type for the happy path, whereas I don't have one. I only want to return something (an error message) if the operation failed.

This tells me that you are not thinking functionally. The functional style to always return something. That something can be a representation of nothing, which is also called Unit.

I think you want the return type to be Either<string, unit>.

@MrYossu
Copy link
Contributor Author

MrYossu commented Feb 8, 2021

This tells me that you are not thinking functionally.
Probably, still trying to hammer this stuff into my thick head!

The functional style to always return something. That something can be a representation of nothing, which is also called Unit.

I think you want the return type to be Either<string, unit>.

OK, thanks. I knew it had to have a simple answer 😀

@MrYossu
Copy link
Contributor Author

MrYossu commented Feb 8, 2021

Quick follow-up if you don't mind.

The previous, not-very-functional incarnation of this method had a return type of Task<(bool, string)>, which I was using as follows...

  (bool success, string errors) = await ViewModel.OnSubmitPersonalDetails(ec);
  if (success) {
    NavManager.NavigateTo("/Investors");
  } else {
    _inform.Display("Error saving the details: " + errors);
  }

NavManager.NavigateTo is a void method that navigates to another page (this is Blazor), and _inform.Display is a void method on a Blazor component.

Now I have changed the method to return an Either<string, Unit>, how do I do this?

I tried the following..

  Either<string, Unit> result = await ViewModel.OnSubmitPersonalDetails(ec);
  result.Match(
    NavManager.NavigateTo("/Investors"),
    errors => _inform.Display("Error saving the details: " + errors)
  );

...but that gave a compiler error "cannot convert from 'void' to 'System.Action<LanguageExt.Unit>'" on the line that calls NavManager.NavigateTo.

What should I do? Thanks again.

@MrYossu
Copy link
Contributor Author

MrYossu commented Feb 8, 2021

Duh, just realised that it should be this...

  result.Match(
    _ => NavManager.NavigateTo("/Investors"),
    errors => _inform.Display("Error saving the details: " + errors)
  );

Is that right? Thanks again.

@TysonMN
Copy link
Contributor

TysonMN commented Feb 8, 2021

Yes, that looks correct to me.

@MrYossu
Copy link
Contributor Author

MrYossu commented Feb 8, 2021

thanks

@MrYossu MrYossu closed this as completed Mar 15, 2021
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

2 participants