-
Notifications
You must be signed in to change notification settings - Fork 59
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
Evaluation usage #47
Comments
Hi @ibcoleman – don't worry, you haven't painted yourself into a corner! I hope you don't mind that I created a new issue for this. Hopefully later today I'll have the time to put together some examples of how to deal with the situation you are in and post them here! |
Hi again @ibcoleman! Here's a small example that maybe helps you achieve what you want?: abstract class Failure {}
class PointlessDepartment extends Failure {}
class UnexpectedFailure extends Failure {
final Object cause;
UnexpectedFailure(this.cause);
}
Future<Either<Failure, IList<int>>> getEmployeeIds(String departmentName) async {
final lowercaseDepartmentName = departmentName.toLowerCase(); // I sure hope departmentName isn't null!
if (lowercaseDepartmentName == "r&d") {
return right(ilist([1, 2, 3]));
} else {
return left(PointlessDepartment());
}
}
final M = EvaluationMonad<Failure, Unit, Unit, Unit>(UnitMi);
Evaluation<Failure, Unit, Unit, Unit, A> liftFE<A>(Future<Either<Failure, A>> fea) => M
.liftFuture(fea.catchError((cause) => left<Failure, A>(UnexpectedFailure(cause))))
.bind(M.liftEither);
void main() async {
final successful = await liftFE(getEmployeeIds("R&D")).value(unit, unit);
final expectedFailure = await liftFE(getEmployeeIds("HR")).value(unit, unit);
final unexpectedFailure = await liftFE(getEmployeeIds(null)).value(unit, unit);
assert(successful == right(ilist([1,2,3])));
assert(expectedFailure.fold((err) => err is PointlessDepartment, (_) => false));
assert(unexpectedFailure.fold((err) => err is UnexpectedFailure && err.cause is NoSuchMethodError, (_) => false));
} The Hope that helps! |
Thanks so much @spebbe! I'm going to go away and ponder this... :) |
Closing this for now – feel free to open again if needed! |
I've been playing with EvaluationMonad over time, and really finding it cleans things up considerably. Based on your invaluable responses, I'm basically composing my functions that return Either or Future as follows:
One thing I haven't quite figured out is how to "add" error handling to an either. So in the case above, Obivously the following is kind of screwed up, but I'm taking the String -> String function
|
Hi! I've been messing around with EvaluationMonad and Evaluation for a few days now. One thing I just can't seem to figure out:
In my code, it's obviously common that I have a function that returns a
Future<Either<MyException, MyValue>>
. I can understand the use anEvaluation
in the case where you have anEither<MyException, MyValue>
(just M.liftEither!), or in the case where you have aFuture<MyValue>
(just M.liftFuture!), but I'm really having a hard time understanding how you can make use of it in the case were you're dealing with aFuture<Either<A, B>>
. I'm guessing I would need to refactor those functions that return Future, to return Future and somehow use the EvaluationMonad to set the E value on the Monad?I looked through evaluation_test.dart in the repository, and there's a fairly clear example of liftFuture, and a fairly clear example of liftEither. But they're both completely distinct which makes me think there's no way out of the corner I've painted myself into? :)
Originally posted by @ibcoleman in #36 (comment)
The text was updated successfully, but these errors were encountered: