Implicit conversion from Scala function to Java Function

Mifeet

I would like to create an implicit conversion from a Scala function (possibly anonymous) to java.util.function.Function. Here is what I have:

import java.util.function.{Function => JavaFunction}
implicit def scalaFunctionToJavaFunction[From, To](function: (From) => To): JavaFunction[From, To] = {
  new java.util.function.Function[From, To] {
    override def apply(input: From): To = function(input)
  }
}

It works fine except that the type inference fails when the function being converted doesn't specify parameter type explicitly:

val converted: JavaFunction[String, Int] = (s: String) => s.toInt // works fine
val converted2: JavaFunction[String, Int] = scalaFunctionToJavaFunction(s => s.toInt) // works
val converted3: JavaFunction[String, Int] = s => s.toInt // gives compilation error "missing parameter type"

The compiler is able to infer the type

My questions are:

  • Why cannot Scala compiler infer type of the parameter in the third case?
  • Can I modify the implicit conversion so that the type gets inferred?

I'm aware of a related question which touches on this subject but doesn't give an answer to my questions.

The problem doesn't seem to be related to interoperability with Java, btw. If I replace JavaFunction with a custom Scala trait, the behavior remains the same.

Alexey Romanov

Why cannot Scala compiler infer type of the parameter in the third case?

To infer type(s) of parameter(s) of a lambda-expression, the expected type must be String => ? (for this case). In converted3, the expected type is JavaFunction[String, Int]. This isn't a Scala function, so it won't work. It will in Scala 2.12, or in 2.11 with -Xexperimental scalac option. This is according to the specification:

If the expected type of the anonymous function is of the shape scala.Functionn[S1,…,Sn, R], or can be SAM-converted to such a function type, the type Ti of a parameter xi can be omitted, as far as Si is defined in the expected type, and Ti = Si is assumed. Furthermore, the expected type when type checking e is R.

If there is no expected type for the function literal, all formal parameter types Ti must be specified explicitly, and the expected type of e is undefined.

The "SAM-converted" part is the one activated by -Xexperimental/2.12 and not valid in 2.11 by default.

Can I modify the implicit conversion so that the type gets inferred?

I don't believe so. What you can do is to change the place conversion happens: write val unconverted: String => Int = s => s.toInt (or just val unconverted = (s: String) => s.toInt) and pass it where JavaFunction[String, Int] is expected.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Difference between conversion with implicit function and implicit class in Scala

From Dev

implicit conversion from scala Set to Java Set

From Dev

Cost of implicit conversion from java to scala collections

From Dev

Cost of implicit conversion from java to scala collections

From Dev

Implicit conversion of function type

From Dev

Generalize implicit function in scala

From Dev

implicit conversion sequence in function overloading

From Dev

std::function implicit type conversion

From Dev

Scala - currying function with implicit value

From Dev

Scala Function to create a conversion function from a string to a matched value

From Dev

Implicit Java to Scala collection conversion does not work

From Dev

Implicit Java to Scala collection conversion does not work

From Dev

Call function from Scala in Java

From Dev

Why can't I implicit convert Scala's Function1 to java.util.function.Function?

From Dev

Scala implicit conversion on call-by-name parameter works differently depending on the function is overloaded or not

From Dev

Pass Scala Seq into Scala function from Java

From Dev

Pass Scala Seq into Scala function from Java

From Dev

Ambiguity in calling function. Implicit conversion?

From Dev

groovy implicit function parameter type conversion

From Dev

Will an Implicit conversion function to self ever get called?

From Dev

Why is a malformed function used instead of an implicit conversion?

From Dev

Generic function with generic class: no implicit reference conversion

From Dev

Will an Implicit conversion function to self ever get called?

From Dev

Failure of implicit conversion to number in median function in Oracle

From Dev

Scala - Implicit conversion to implicit argument

From Dev

Scala implicit conversion of implicit argument

From Dev

From implicit to explicit function definitions

From Dev

Scala map with implicit conversion

From Dev

Ducktyping implicit conversion in Scala

Related Related

  1. 1

    Difference between conversion with implicit function and implicit class in Scala

  2. 2

    implicit conversion from scala Set to Java Set

  3. 3

    Cost of implicit conversion from java to scala collections

  4. 4

    Cost of implicit conversion from java to scala collections

  5. 5

    Implicit conversion of function type

  6. 6

    Generalize implicit function in scala

  7. 7

    implicit conversion sequence in function overloading

  8. 8

    std::function implicit type conversion

  9. 9

    Scala - currying function with implicit value

  10. 10

    Scala Function to create a conversion function from a string to a matched value

  11. 11

    Implicit Java to Scala collection conversion does not work

  12. 12

    Implicit Java to Scala collection conversion does not work

  13. 13

    Call function from Scala in Java

  14. 14

    Why can't I implicit convert Scala's Function1 to java.util.function.Function?

  15. 15

    Scala implicit conversion on call-by-name parameter works differently depending on the function is overloaded or not

  16. 16

    Pass Scala Seq into Scala function from Java

  17. 17

    Pass Scala Seq into Scala function from Java

  18. 18

    Ambiguity in calling function. Implicit conversion?

  19. 19

    groovy implicit function parameter type conversion

  20. 20

    Will an Implicit conversion function to self ever get called?

  21. 21

    Why is a malformed function used instead of an implicit conversion?

  22. 22

    Generic function with generic class: no implicit reference conversion

  23. 23

    Will an Implicit conversion function to self ever get called?

  24. 24

    Failure of implicit conversion to number in median function in Oracle

  25. 25

    Scala - Implicit conversion to implicit argument

  26. 26

    Scala implicit conversion of implicit argument

  27. 27

    From implicit to explicit function definitions

  28. 28

    Scala map with implicit conversion

  29. 29

    Ducktyping implicit conversion in Scala

HotTag

Archive