-
Notifications
You must be signed in to change notification settings - Fork 51
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 chain operations with Try<Option>? #254
Comments
I'll take a look at this later today. V2 of cyclops-react will provide away to keep the types available (using Witness types and fluent type safe conversion methods). In v1 AnyM drops the monad type and keeps only the type of the contained value. There is an implementation of FutureT in v2 already, when the API is solid it can be expanded for other types. I'll add these examples as tests and investigate. |
I haven't run this yet, but from the code this should return a Try.Failure wrapped in an AnyM. Looks like a bug. |
The bug seems to in in the toString() generation as the result of the flatMapT operation does seem to have the correct type. E.g. the intermediate variable v is displayed as OptionalTValue[AnyMValue[]], but when we drill down into it in the debugger we can see it has the expected type. I'll work on fixing the toString() on the 1.x branch. Improving the type safety + simplicity of this is a big driver behind the motivation of the next big cyclops release (2.x for cyclops-react and will be 9.x for the integration modules). In 2.x the code should look more like this OptionalT<tryType,String> valueT1 = Optionals.liftM(Optional.of("Noor1"),Witness.tryType.INSTANCE);
OptionalT<tryType,String> valueT2 = OptionalT.fromValue(Try.failure(new RuntimeException("ERROR")));
OptionalT<tryType,String> valueT3 = valueT1.flatMapT(i-> valueT2.map(j -> i.concat(j)));
Try<Optional<String>,Throwable> tryOpt = valueT3.unwrapTo(Witness::tryType); But with v1 the best we can do is OptionalTValue<String> valueT1 = OptionalT.fromValue(Try.of(Optional.of("Noor1")));
OptionalTValue<String> valueT2 = OptionalT.fromValue(Try.failure(new RuntimeException("ERROR")));
Try<Optional<String>,Throwable> v = valueT1.flatMapT(i-> valueT2.map(j -> i.concat(j)))
.unwrap()
.unwrap();
System.out.println(v); Which isn't type safe (as via unwrap().unwrap() we know we have Optional
|
Thanks a million for reporting this by the way! If you find any other issues or have any questions please do log an issue or get in touch. |
No problem, it is my pleasure. You are doing a tremendous job. One more problem that I faced to get start is which version to start with. The documention probably is refering to version 1 where as version 2 seems more attractive. Considering the requirement I have in hand which involves multiple Monads (Option, Try, Reader etc) and Applicative functors like Validation, please recommend me most suitable verision. Also, I may use Project Reactor in near future. |
I think v2 is a big improvement from v1 - but most of the monad transformers have yet to be reimplemented in the new way. Also cyclops-reactor has to be reworked for v2. On balance I'd recommend going with v1 for now, but keep an eye on v2. Very good progress on v2 during the week (just not enough to recommend that you go with it over v1 at this stage). |
Hi,
I tried chaining below operations
And it prints below string instead of OptionalTValue[AnyMValue[Failure]]
OptionalTValue[AnyMValue[]]
Also, is there any possibility to keep the value type as below:
The text was updated successfully, but these errors were encountered: