https://kotlinlang.org logo
#announcements
Title
# announcements
e

ehonsey

02/09/2017, 11:35 PM
Has anyone seen a bug on type inference for java classes with same short names? I don't want to file a duplicate bug. However I had an issue assigning a field with type of
java.sql.Date
. Although I have an import for that class in the calling class, when I call
Date.from(Instant)
the compiler is inferring
java.util.Date
instead of
java.sql.Date
. I had to give a type hint. I would have expected the compiler to make this inference based on the type of the target field.
i

ilya.gorbunov

02/10/2017, 12:29 AM
ehonsey: Could you post a small code example to reproduce that?
e

ehonsey

02/10/2017, 3:48 PM
Copy code
import java.sql.Date
import java.time.Instant

class SqlDateWrapper(sqlDate: Date)

fun typeInferenceError() {
    val wrapper = SqlDateWrapper(Date.from(Instant.now()))
}
The class takes a type of java.sql.Date, but my IntelliJ is showing a compiler error on
Date.from
. My original Kotlin compiler version was 1.0.4. However, the same error showed on 1.0.6
Actually, that might not be an error. I thought java.sql.Date had an override for that method, but it is actually part of the superclass java.util.Date. So the compiler is acting normally in this case. I don't think this is an issue. Sorry for the red herring
i

ilya.gorbunov

02/10/2017, 3:55 PM
java.sql.Date
extends
java.util.Date
. There is no static
from
method in JDK, that returns
java.sql.Date
, only the one that returns
java.util.Date
. So when you call
java.sql.Date.from
, you actually refer to
java.util.Date.from
, and thus you get
java.util.Date
as a result.
I wonder who designed this API mess 🙄
e

ehonsey

02/10/2017, 3:59 PM
Right? I just realized how difficult it is to go from an
java.time.Instant
to a
java.sql.Date
for my JPA entity. Ugh
Thank you for your assistance, by the way 👍
3 Views