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

How to create Result<void, Failure>? #10

Open
AlexanderFarkas opened this issue Mar 29, 2021 · 7 comments
Open

How to create Result<void, Failure>? #10

AlexanderFarkas opened this issue Mar 29, 2021 · 7 comments

Comments

@AlexanderFarkas
Copy link

AlexanderFarkas commented Mar 29, 2021

It's not allowed to leave Result.ok empty. How to achieve void behaviour? Null is pretty awful workaround.

@nlfiedler
Copy link
Owner

From what I can tell, Dart does not have a "unit" type like Rust, and void is "special". What's more, the Void type in dart:ffi can only be used in the Dart VM, and worse, it cannot be instantiated. The only way I can see to create a Result with a void value type is with a void function and use Result.of(), like so:

void func() {
  return;
}
var result = Result.of(() => func());

The type of result is Result<void, dynamic>

@AlexanderFarkas
Copy link
Author

Couldn't you create Result.okVoid, which doesn't return anything?

@nlfiedler
Copy link
Owner

I tried a few things and nothing would pass the compiler, it insists that the value must be of type T and neither null nor void can satisfy that requirement. Technically the example I gave earlier isn't quite right, but at least it compiles without issue.

@lemunozm
Copy link
Contributor

For that issue, Dartz library (which has a kind of Result called Either) offers an Unit type that makes the trick and is very visual throughout the code. Maybe something similar can be implemented here.

@nlfiedler
Copy link
Owner

Let me know if commit a692c01 is what you're looking for.

@Stumblinbear
Copy link
Contributor

Stumblinbear commented Feb 10, 2023

Couldn't you do this by removing the extends Object bound on all of the generics (except for E)? I don't see any particular reason why they're restricted to Object.

Removing them shouldn't break existing code (as loosening bounds shouldn't do that), and would permit Result<void, E> and returning Result.ok(null) instead of the unit type. I've tested this in a local dependency_override and it seems to work fine.

@nlfiedler
Copy link
Owner

@Stumblinbear Your contributions would be welcome, please feel free to submit a PR.

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

4 participants