def fn[String] seems to break Scala / java.lang.String compatibility
generics, scala, type-inference
Solution
When you write:
def fail[String](fn: (String) => Unit) = fn("")
The type parameter between square brackets `String` is just an arbitrary name that, in your case, will hide scala or java string. It is fully equivalent to:
def fail[T](fn: (T) => Unit) = fn("")
If you want to constrain the type to string, you just have to write:
def fail(fn: (String) => Unit) = fn("")
And it will work for scala and java strings (since they are the same).
Problem
Hello there Stack Overflow, I hope you'll help me with my very first question here :) So I'm having a problem with Scala type inference. Here is the code: ``` object Problem { def ok(fn: (String) => Unit) = fn("") // type mismatch; found: java.lang.String("") required: String def fail[String](fn: (String) => Unit) = fn("") } ``` What kind of String does Scala expect here? Note that this is a minimal example to explain my problem. The original issue appeared when I tried to implement a more complex interface (Play's Iteratee, to be precise), so, no, leaving out the `[String]` is not an option. (If anyone thinks that the actual code would help, I'll provide it.) I tried `def fail[java.lang.String] ...` but then it says `expected ], found .`. I did read Scala String vs java.lang.String - type inference which gives a good explanation on `java.lang.String` vs. `scala.Predef.String`, but I still could not come up with a solution for my specific problem. Any ideas? EDIT: So here is the original attempt how I tried to implement http://www.playframework.org/documentation/api/2.0/scala/play/api/libs/iteratee/Iteratee.html only that I wrote `String` instead of `T`. (With `T` it compiles, and it makes sense!) My fail; obviously I was a bit overwhelmed by all the type parameters: ``` val stream = WS.url("url").get({ headers => (new Iteratee[Array[Byte], String] { def fold[T](done: (String, Input[Array[Byte]]) => Promise[T], cont: (Input[Array[Byte]] => Iteratee[Array[Byte], String]) => Promise[T], error: (String, Input[Array[Byte]]) => Promise[T]): Promise[T] = { done("something", Input.Empty) } }) }) ``` Regards, Hendrik