Method Pointers and Partial Calls

Method Pointers

Often in functional languages you want to pass functions to other functions. For example, the map function in several functional languages takes a function and a list, and applies the function to each member of the list, returning a list of the results.

JFuncMachine provides a way to make a pointers to methods and constructors. When a pointer points to an instance or an interface method, you have the option to supply the object that contains the method or not. If you do not supply the target object the lambda generated by the method pointer will expect the object to be passed in as the first parameter.

The methods to create method pointers are contained in the MethodPtrs class, which has utility methods that just create lambdas to invoke the desired method. JFuncMachine provides utilities for pointers to Java methods:

  • makeJavaConstructorPtr
  • makeJavaMethodPtr,
  • makeJavaInterfaceMethodPtr
  • makeJavaStaticMethodPtr

There are also pointers to methods that may use JFuncMachine’s tail call optimization:

  • makeMethodPtr
  • makeStaticMethodPtr

Partial Functions

A partial function call is basically a pointer to a method with some of the arguments already supplied. You can make a partial function call on instance and static methods. As with the method pointers, you have the option whether to supply the target object or not. If you do not supply one, then the generated lambda will take the target object as the first parameter.

JFuncMachine provides utilities for partial calls to Java methods:

  • makePartialJavaConstructorCall
  • makePartialJavaMethodCall,
  • makePartialJavaInterfaceMethodCall
  • makePartialJavaStaticMethodCall

There are also partial calls to methods that may use JFuncMachine’s tail call optimization:

  • makePartialMethodCall
  • makePartialStaticMethodCall

You can also make partial calls to method pointers with makePartialInvoke.

Again, it is possible to make a partial invoke that still requires the target object to be passed in, but in this case it requires a separate utility method named makePartialInvokeWithoutObject.