```fun RequestInit(request: Request): RequestInit ...
# eap
m
Copy code
fun RequestInit(request: Request): RequestInit =
	Object.assign(jsObject(), request)
Why does that work in Kotlin 1.4 but fails in 1.5.0-RC? Is that intentional?
[TYPE_MISMATCH] Type mismatch.
Required: RequestInit
Found: Request
t
It looks intentional. AFAIK
Promise.then
contract fix require such behaviour
b
how and where
assign
is defined?
m
kotlin-extensions
library
1.0.1-pre.148-kotlin-1.4.30
Copy code
fun <T : Any, R : T> assign(dest: R, vararg src: T?): R
🙏 1
b
Self contained example, doesn’t work since 1.5.0-M2:
Copy code
fun <T : Any, R : T> myassign(dest: R, vararg src: T?): R = TODO()

fun <T : Any> myjsObject(): T = TODO()

fun RequestInit(request: Request): RequestInit = myassign(myjsObject(), request)
☝️ 1
@Victor Petukhov please take a look
v
@Marc Knaup thanks for the report. The problem is the same as here. Such code is actually incorrect because for this example:
Copy code
class Request
class RequestInit

fun <T : Any, R : T> myassign(dest: R, vararg src: T?): R = TODO()

fun <T : Any> myjsObject(): T = TODO()

fun RequestInit(request: Request): RequestInit = myassign(myjsObject(), request)
expected type (
RequestInit
) isn’t actually subtype of
Request
as should be by looking at declarations. Also it can be exploited to go though type system hole and get runtime exception without any compile time errors:
Copy code
class Request(var x: Int)
class RequestInit {
    fun isRequestInit() {}
}

fun <T, R : T> myassign(dest: R, src: T): R = dest

inline fun <reified T : Request> myjsObject(): T = T::class.createInstance()

fun getRequestInit(request: Request): RequestInit = myassign(myjsObject(), request) // `myjsObject()` has `{Request & RequestInit}` type

fun main() {
    getRequestInit(Request(1)).isRequestInit() // CCE: java.lang.Object cannot be cast to Request
}
So the change in the compiler is intended (it was really introduced recently), and source code’s fix is needed.
🙏 1
m
Interesting. Does that mean that the definition of
assign
is incorrect? I don’t understand the reason for the
R : T
constraint anyway.
v
You have two ways to fix it: change `assign`’s declaration by removing
R : T
or change use site – actual arguments by passing only arguments which satisfy to upper bound constraint.
m
Well solution #1 is up for JetBrains to fix – if that doesn’t cause other issues. I’ve done #2 like this:
Copy code
fun RequestInit(request: Request): RequestInit =
	Object.assign(jsObject<RequestInit>(), request)
Thanks for the feedback and explanation 🙏
v
BTW during investigation affected area by this fix in the compiler, we’ve discovered pretty similar issue in the Corda source code: https://github.com/corda/corda/issues/6888
m
No idea what that is though 🙂 But yeah, +1 on improving type safety.
b
It looks like it couldn’t express in Kotlin more precise than
Copy code
fun <D, R : D> assign(dest: D, vararg src: Any?): R
👌 1
cc @Akif Abasov [JB] @Leonid Khachaturov
t
Or
dynamic
sources?
Copy code
fun <D, R : D> assign(dest: D, vararg src: dynamic): R
l