0

I have run into an issue with functional programming since I want to be able to get the method name, but it's not so easy to do as it is with OOP. It makes my code very clean to stay in functional programming, so I'm wondering if I can achieve what I want (sampled below) without switching over to OOP

public interface FunctionalInterface{
   boolean satisfied(String v1)
}

public class functionalClass{
   public static FunctionalInterface myMethod(){
       satisfied(String v1) -> return v1.equals("yes");
   }
   public static FunctionalInterface myMethod2(){
       satisfied(String v1) -> return vi.equals("no");
   }
}

public class functionalClassCaller{
   private final Set<FunctionalInterface> aFunctionalInterfaces = new HashSet<>();

   public boolean satisfied(String v1){
   for( FunctionalInterface functionalInterface : aFunctionalInterfaces )
        {
            if( !aFunctionalInterface.satisfied(v1))
            {
                 //I want to know which FunctionalInterface method returns false 

            }
        }

   }
}
biopython
  • 13
  • 2
  • Unrelated: read about java naming conventions. It should be `FunctionalClass`, and rather `isSatisfied`. But then: I dont understand your question. What has functional programming to do with getting to "the method name"? In the end, you have two anonymous inner classes there, and you ONLY know about them that they implement that interface. So, whats the point, what "more" do you want to know? Or assume you could know? – GhostCat Apr 13 '21 at 06:32
  • @GhostCat This is actually just a sample I just coded to make it easier to visualize my problem. When I call let's say functionalInterface.toString() inside the satisfied method, I'll get something like functionalClass$$Lambda$550/0x0000000800dc99f0@51243dec. When what I want is actually "MyMethod" – biopython Apr 13 '21 at 06:38
  • What you are asking for isn't possible. So maybe you should step back and consider to ask (maybe in another question): "here is the problem I need to solve, how to do that in java". Because what you are asking for here very much sounds like https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem – GhostCat Apr 13 '21 at 06:43

1 Answers1

0

Unfortunately, the answer is: not possible!

You see:

public static FunctionalInterface myMethod(){
  satisfied(String v1) -> return v1.equals("yes");
}

returns an object of some anonymous inner class. That object doesn't know anything about the scope in which it was created/instantiated! Neither the java compiler nor the java runtime know/care associate newly created objects with the "name" of the method in which that happened (besides information that needs to sit on the method's stack).

Meaning: if your interface instances need some "additional context information", then the interface itself needs to provide that information, like:

public interface FunctionalInterface{
  boolean satisfied(String v1);
  Context getInterfaceCreationContext();

or something alike!

GhostCat
  • 137,827
  • 25
  • 176
  • 248