As far as I understand, in Scala
String
is an alias forjava.lang.String
as explained here, and can be seen in Predef.scala:
type String = java.lang.String
So essentially, they are the same.
However, in IntelliJ IDEA 2017.2.5, the below code produces an error:
trait PerceptualHash {
def calc[A](bi: BufferedImage): A
}
sealed trait BinaryStringPerceptualHash extends PerceptualHash {
override def calc[String](bi: BufferedImage): String
}
private object GeneralBinaryStringPerceptualHash extends BinaryStringPerceptualHash {
def calc[String](bi: BufferedImage): String = "0"
}
Here, "0"
gets underlined with message "Expression of type java.lang.String does not conform to expected type String".
But if I change "0"
like so:
def calc[String](bi: BufferedImage): String = new String("0")
then I get no such error message.
What's going on? Is it expected behaviour (and if so, why), am I doing something wrong, or rather it is a type inference bug in Intellij IDEA?