Allow Java functional interfaces and SAMs to be wrapped and used as functions

Description

In order for this to work, the argument must be typed not just as function but with the specific FI/SAM interface specified as function:Predicate.

Doing so via castas would look like this

A function argument would look like so:

The example above would accept EITHER a first-class BoxLang function OR a java class implementing the java.util.function.Producer functional interface.

This will allow any higher order function to accept either a Boxlang function or an instance of a Java functional interface or SAM which provides greater interop with pre-built Java FIs or JSR-223 integration.

For all known functional interfaces in the JDK, you can specify just the class name and the name is not case sensitive. Here is the current list of “known” functional interfaces that allow a shortcut:

  • java.util.function.BiConsumer

  • java.util.function.Consumer

  • java.util.function.DoubleConsumer

  • java.util.function.IntConsumer

  • java.util.function.LongConsumer

  • java.util.function.ObjDoubleConsumer

  • java.util.function.ObjIntConsumer

  • java.util.function.ObjLongConsumer

  • java.util.function.DoubleBinaryOperator

  • java.util.function.DoubleFunction

  • java.util.function.DoubleToIntFunction

  • java.util.function.DoubleToLongFunction

  • java.util.function.UnaryOperator

  • java.util.function.BinaryOperator

  • java.util.function.DoubleUnaryOperator

  • java.util.function.Function

  • java.util.function.IntBinaryOperator

  • java.util.function.IntFunction

  • java.util.function.IntToDoubleFunction

  • java.util.function.IntToLongFunction

  • java.util.function.IntUnaryOperator

  • java.util.function.LongBinaryOperator

  • java.util.function.LongFunction

  • java.util.function.LongToDoubleFunction

  • java.util.function.LongToIntFunction

  • java.util.function.LongUnaryOperator

  • java.util.function.BiFunction

  • java.util.function.Predicate

  • java.util.function.BiPredicate

  • java.util.function.Supplier

  • java.util.Comparator

ANY functional interface or SAM (single abstract method) can be specified, but you’ll need to

  • specify the full case sensitive path to the SAM interface

  • The interface will need to be available on the class path

Ex:

Activity

Show:

Brad Wood July 18, 2024 at 7:07 AM

The issue described in the comment above has been fixed. All of the built in higher order BIFs in BoxLang will detect if they are invoking a wrapped java method. If so, they will not pass additional args. So in the case of arrayEach(), BoxLang callbacks will receive 3 args, but java callbacks will only receive 1. This will allow better interop with Java libs which do not allow extra args.

Brad Wood July 11, 2024 at 12:55 AM

All higher order BIFs will not pass the extra arguments that CF/BL callbacks receive. So for example,

the callback will normally receive 3 args (the value the index, and the array) but when passing a java FI or SAM, the callback will only receive 1 argument (the value) to match the standard behavior of Java libraries so a standard consumer can be used here without getting errors for too many args.

Fixed
Pinned fields
Click on the next to a field label to start pinning.

Details

Assignee

Reporter

Fix versions

Priority

Sentry

Created July 11, 2024 at 12:50 AM
Updated July 18, 2024 at 7:07 AM
Resolved July 11, 2024 at 12:50 AM