-4
type mismatch;
   found   : String => Int
   required: java.util.function.ToIntFunction[_ >: String]

I really do wish before people wrote up the "next big thing" they got a few language specialists on board who could explain to them that error messages can be one of two things

  1. a meaningless code
  2. a fully formed sentence that explains the issue to someone who doesn't understand the language.

The rational being, if you understand the language (eg read the manual), then you just need an error code. But if you don't understand the language (ie the person who is going to get the error message in the first place), then you need a full sentence with explanations, sample problem and solution.

Think of the time saved - the total time of all the developers who might make the error - vs the time cost ( a developer + language specialist writing an explanatory sentence with example ).

Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66
bharal
  • 15,461
  • 36
  • 117
  • 195
  • 3
    @bharal [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) would be more helpful than philosophical part. – Dmytro Mitin Sep 28 '20 at 12:57
  • 2
    @bharal _"why not just make the language better?"_ what would it be better for you? the error is clear you have an `String => Int` and you need a `java.util.function.ToIntFunction[_ >: String]`, if you do not understand the difference between two classes then no error message in the world would help you there. - _"all the developers who have to learn some arbitrary logic that isn't explained"_ care to express which arbietray logic that is not explained is present in this example? - _" the time loss of writing clearer explanations"_ this is not as easy as you think, but give me a better message – Luis Miguel Mejía Suárez Sep 28 '20 at 13:13
  • @bharal By the way, continuing this off-topic, which language would you like to improve? Java or Scala? Do you realize that regarding languages interop only one of languages being improved can be not enough? – Dmytro Mitin Sep 28 '20 at 13:18
  • 1
    @bharal Actually language **has** been improved since in 2.12-2.13 this should work. It's just you using not the latest version (or being not able to use the latest one because of library dependencies maybe). – Dmytro Mitin Sep 28 '20 at 13:23
  • @LuisMiguelMejíaSuárez The error can give an example of what would cause such a message, and how to resolve it. The error can explain what the various symbols mean. You don't understand, so let me try again. In the old days, error messages were just codes that you had to look up explanations for. Modern languages now have error messages that are the explanations - bypassing the error code. But the point was not to save developer time looking up an error code. The point is to get the developer building. So instead, now the developer *looks up the message itself*. Eg what i did. – bharal Sep 28 '20 at 13:49
  • @LuisMiguelMejíaSuárez you arguing that i should "just know" is as neat and basic an idea as "all developers should have the error codes memorised". This is of course silly, nobody is using error codes anymore, and nobody should couch the error message *in the language of the program itself*. Because either you're assuming the end user is so well versed they understand the issue - in which case, use an error code. Or you're assuming they end user is not familiar, in which case give a full throated explanation – bharal Sep 28 '20 at 13:51
  • @LuisMiguelMejíaSuárez to continue, a better message i cannot reliably give, as i still do not understand the issue - i know i can just update scala to 2.12 and it solves it. But from Dmytro's answer, i see that you could write "This is caused when using scala and java type functions in the same line of code. This will be addressed in an updated version of scala, for now use the FuncitonConverters._ thing. eg: – bharal Sep 28 '20 at 13:54
  • @DmytroMitin this is a scala error, scala runs on java, so i'd guess it would be a scala error message. But i'm not really clear on the details. – bharal Sep 28 '20 at 13:55
  • @bharal Scala doesn't run on Java (or more precisely, normally it doesn't). Both Scala and Java are compiled to bytecode that runs in JVM. – Dmytro Mitin Sep 28 '20 at 13:57
  • @DmytroMitin yeh, scala runs on java. – bharal Sep 28 '20 at 14:23
  • 2
    @bharal So you do not differ Java the language (like Scala), Java bytecode, and Java virtual machine. – Dmytro Mitin Sep 28 '20 at 14:28
  • 2
    @bharal _"The error can give an example of what would cause such a message, and how to resolve it."_ the error points to the exact line that causes such a message; providing arbitrary examples is not only very difficult but mostly useless since you have the concrete code that failed. About how to fix it, again in the general sense that is very complex, there could be an infinite amount of solutions; however, if you want, search for something called **typed holes** which is somewhat close to what you want. – Luis Miguel Mejía Suárez Sep 28 '20 at 14:54
  • 1
    _"The error can explain what the various symbols mean"_ the only symbol in the error message is `=>`, which is the standard symbol for **function** in Scala. Again, if you would have taken the time to do a basic tutorial to introduce you to the language, you would know it. - finally, you keep talking like you are a super experienced developer, but you can not understand that two classes / types are not the same. So the error message _"expected Int found String"_ is too difficult for you? Now, again your suggestion is either too specific to Scala-Java interop or impossible in the general sense. – Luis Miguel Mejía Suárez Sep 28 '20 at 14:58
  • 1
    Now, if would understand the error message you could have simply googled _"convert scala function to java function"_ and you would find a lot of useful resources. I am not saying you should know how to solve the problem, I am saying that understanding the error is not difficult as you say, you just needed to have invested an hour or so of your time studying the language. – Luis Miguel Mejía Suárez Sep 28 '20 at 15:01
  • 1
    Finally, I really do not see a problem with you asking a question if you do not understand what is happening, we would happily explain the problem and help you fix it, but you started to rant without any reason; and that is where the problem begins because you are talking like you are an expert but you can not read plain English, and suggest solutions that are completely impossible to achieve. – Luis Miguel Mejía Suárez Sep 28 '20 at 15:07

1 Answers1

2

String => Int is scala.Function1[String, Int] i.e. a Scala function type.

java.util.function.ToIntFunction[String] is a Java function type.

In Scala 2.12+ they are supposed to be interchangeable, in earlier versions of Scala you should convert one to another.

How to use Java lambdas in Scala (answer #2)

ToIntFunction[_ >: String] means that the situation can be a little more complicated because it involves existential types but it's hard to say better without reproducible code sample.


Try

//libraryDependencies += "org.scala-lang.modules" %% "scala-java8-compat" % "0.9.1"
import scala.compat.java8.FunctionConverters._
x.get("answers").findValuesAsText("id").stream().mapToInt(((valx:String) => Integer.parseInt(valx)).asJava).max().getAsInt

Does it work or does error message change?

Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66
  • neat i just went to scala 2.12. I don't really understand what i would do in 2.11 if i was stuck with it though – bharal Sep 28 '20 at 12:54
  • I'm using this x.get("answers").findValuesAsText("id").stream().mapToInt( (valx:String) => Integer.parseInt(valx)).max().getAsInt – bharal Sep 28 '20 at 12:55
  • 3
    @bharal This should be in your question. – Dmytro Mitin Sep 28 '20 at 12:55
  • i guess, but it ends up making the question so specific as to be meaningless for people with a similar problem, but caused by a different issue. – bharal Sep 28 '20 at 12:57
  • 1
    @bharal Different people with the same error message can have different fixes. Generally you can try to add `"org.scala-lang.modules" %% "scala-java8-compat" % "0.9.1"`, `import scala.compat.java8.FunctionConverters._` and do `scalaFunction.asJava`. Your code sample is not minimal reproducible. Now I have to guess what type `x` has, what type `x.....stream()` has. – Dmytro Mitin Sep 28 '20 at 13:05
  • 2
    @bharal It seems you are mixing some **Java** API with **Scala** code, it may help to mention which framework / library you are using. – Luis Miguel Mejía Suárez Sep 28 '20 at 13:10
  • @bharal Try `import scala.compat.java8.FunctionConverters._` `x.get("answers").findValuesAsText("id").stream().mapToInt(((valx:String) => Integer.parseInt(valx)).asJava).max().getAsInt`. Does it work or does error message change? – Dmytro Mitin Sep 28 '20 at 13:14
  • 1
    @bharal So, does `.asJava` work or not? – Dmytro Mitin Sep 28 '20 at 14:29