4

Suppose I have this class:

class Dependency{      
   public Dependency reverse() { ... } // returns this Dependency after some reverse OP

   public int doSomething(int param) { ... }

}

Can I use it like this:

Optional<Integer> opt = ...
Dependency dep = new Dependency();

opt.map(dep::reverse::doSomething);

This would be quite clean, but is it possible? To apply a transformation (reverse) to Dependency and then apply the doSomething method?

2
  • 3
    "Can I use it like this" - no, this wont compile. Double-reference is not a thing. Maybe you intend to do two map calls. opt.map(...).map(...). Not exactly sure though what you are attempting to do here. It would help if you could show a working code snippet, even if it is not as elegant as you would like it to be. Just to understand your scenario. Commented Mar 17, 2020 at 18:19
  • 1
    You say (in the code comment) that reverse() modifies the Dependency instance? Then, why not change the declaration to Dependency dep = new Dependency().reverse();? Commented Mar 19, 2020 at 14:07

1 Answer 1

6

If I understood you correctly, seems that reverse() won't be consuming the Integer, so you could simply do:

opt.map(dep.reverse()::doSomething);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.