Skip to content

Commit

Permalink
Move FunctionConverters to javaapi
Browse files Browse the repository at this point in the history
  • Loading branch information
lrytz committed Apr 24, 2019
1 parent 503113a commit a89783a
Show file tree
Hide file tree
Showing 5 changed files with 1,015 additions and 987 deletions.
90 changes: 49 additions & 41 deletions project/GenerateFunctionConverters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -363,62 +363,70 @@ object GenerateFunctionConverters {

def run(outDir: java.io.File): Unit = {
val (impls, explicitDefs, defss) = SamConversionCode(buildWrappersViaReflection: _*)
val funConvs =
val javaFunConvsNoComments =
s"""/** This object contains methods that convert between Scala and Java function types.
| *
| * The explicit conversion methods defined here are practical when writing Java code. For Scala
| * code, it is recommended to use the extension methods defined in [[FunctionConverters.Ops]].
| * The explicit conversion methods defined here are intended to be used in Java code. For Scala
| * code, it is recommended to use the extension methods defined in [[scala.jdk.FunctionConverters]].
| *
| * Using the `.asJava` extension method (from [[FunctionConverters.Ops]]) on a Scala function
| * produces the most specific possible Java function type:
| * For details how the function converters work, see [[scala.jdk.FunctionConverters]].
| *
| * {{{
| * scala> import scala.jdk.FunctionConverters; import FunctionConverters.Ops._
| * 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)
| * }}}
| */
|object FunctionConverters {
| /** This object provides extension methods that convert between Scala and Java function types,
| * see [[FunctionConverters]].
| */
| object Ops extends Priority0FunctionExtensions
|
| import FunctionWrappers._
| import scala.jdk.FunctionWrappers._
|
|${explicitDefs.indent.mkString("\n")}
|}
""".stripMargin
|}""".stripMargin

// cannot generate comments with quasiquotes
val res = funConvs.replace(""" @deprecated("primitiveComment") """,
val javaFunConvs = javaFunConvsNoComments.replace(""" @deprecated("primitiveComment") """,
s""" /** Note: this method uses the boxed type `java.lang.X` (or `BoxedUnit`) instead of the
| * primitive type `scala.X` to improve compatibility when using it in Java code (the
| * Scala compiler emits `C[Int]` as `C[Object]` in bytecode due to
| * [[https://github.com/scala/bug/issues/4214 scala/bug#4214]]). In Scala code, add
| * `import scala.jdk.FunctionConverters.Ops._` and use the extension methods instead.
| * `import scala.jdk.FunctionConverters._` and use the extension methods instead.
| */
|""".stripMargin + " ").replace("""@deprecated("noComment") """, "")
write(outDir, Artifact("jdk/FunctionConverters.scala", sourceFile("", res)))

val scalaFunConvs =
"""/** 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)
| * }}}
| */
|object FunctionConverters extends Priority0FunctionExtensions""".stripMargin

write(outDir, Artifact("jdk/javaapi/FunctionConverters.scala", sourceFile(".javaapi", javaFunConvs)))
write(outDir, Artifact("jdk/FunctionConverters.scala", sourceFile("", scalaFunConvs)))
write(outDir, Artifact("jdk/FunctionWrappers.scala", sourceFile("", impls.mkString("\n"))))
write(outDir, Artifact("jdk/FunctionExtensions.scala", sourceFile("", defss.map(_.mkString("\n")).mkString("\n\n\n\n"))))
}
Expand Down
Loading

0 comments on commit a89783a

Please sign in to comment.