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 chain operations with Try<Option>? #254

Open
noorulhaq opened this issue Jan 15, 2017 · 6 comments
Open

How to chain operations with Try<Option>? #254

noorulhaq opened this issue Jan 15, 2017 · 6 comments
Labels

Comments

@noorulhaq
Copy link

noorulhaq commented Jan 15, 2017

Hi,

I tried chaining below operations

OptionalTValue<String>  valueT1 = OptionalT.fromValue(Try.of(Optional.of("Noor1")));
OptionalTValue<String> valueT2 = OptionalT.fromValue(Try.failure(new RuntimeException("ERROR")));

System.out.println(valueT1.flatMapT(i-> valueT2.map(j -> i.concat(j))));

And it prints below string instead of OptionalTValue[AnyMValue[Failure]]
OptionalTValue[AnyMValue[]]

Also, is there any possibility to keep the value type as below:

Try<Option<String>>  valueT1 = OptionalT.fromValue(Try.of(Optional.of("Noor1")));
@johnmcclean
Copy link
Member

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.

@johnmcclean
Copy link
Member

I haven't run this yet, but from the code this should return a Try.Failure wrapped in an AnyM. Looks like a bug.

@johnmcclean
Copy link
Member

johnmcclean commented Jan 15, 2017

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).

screen shot 2017-01-15 at 21 13 15

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<String> but not Try), but it does at least print the correct value

Try.Failure(error=java.lang.RuntimeException: ERROR)

@johnmcclean
Copy link
Member

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.

@noorulhaq
Copy link
Author

noorulhaq commented Jan 16, 2017

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.

@johnmcclean
Copy link
Member

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants