FunctionConverters

This object provides extension methods that convert between Scala and Java function types.

When writing Java code, use the explicit conversion methods defined in javaapi.FunctionConverters instead.

Using the .asJava extension method on a Scala function produces the most specific possible Java function type:

scala> import scala.jdk.FunctionConverters._
scala> val f = (x: Int) => x + 1

scala> val jf1 = f.asJava
jf1: java.util.function.IntUnaryOperator = ...

More generic Java function types can be created using the corresponding asJavaXYZ extension method:

scala> val jf2 = f.asJavaFunction
jf2: java.util.function.Function[Int,Int] = ...

scala> val jf3 = f.asJavaUnaryOperator
jf3: java.util.function.UnaryOperator[Int] = ...

Converting a Java function to Scala is done using the asScala extension method:

scala> List(1,2,3).map(jf2.asScala)
res1: List[Int] = List(2, 3, 4)
Source:
FunctionConverters.scala

Implicits

Inherited implicits

implicit def enrichAsJavaBinaryOperator[T, A1, A2](sf: (T, A1) => A2)(implicit evA1: A1 =:= T, evA2: A2 =:= T): RichFunction2AsBinaryOperator[T]
implicit def enrichAsJavaIntBinaryOperator[A0, A1](sf: (A0, A1) => Int)(implicit evA0: A0 =:= Int, evA1: A1 =:= Int): RichFunction2AsIntBinaryOperator
implicit def enrichAsJavaIntFunction[A0, R](sf: A0 => R)(implicit evA0: A0 =:= Int): RichFunction1AsIntFunction[R]
implicit def enrichAsJavaUnaryOperator[T, A1](sf: T => A1)(implicit evA1: A1 =:= T): RichFunction1AsUnaryOperator[T]