edrd
06/06/2024, 12:00 PMClassCastException
that I’m almost sure is being caused by a Kotlin compiler bug but I’m having a hard time confirming it’s not in http4k, particularly due to my lack of familiarity with the lens concepts. Can you help me figure this out so I know who I should report this to?
Here’s the smallest reproducer I could get:
import org.http4k.core.*
import org.http4k.lens.contentType
fun main() {
class CustomResponse : Response by Response(Status.OK)
CustomResponse()
.contentType(ContentType.APPLICATION_JSON)
.let { println(it) } // Oddly enough, .let(::println) doesn't cause the bug
}
It throws:
java.lang.ClassCastException: class org.http4k.core.MemoryResponse cannot be cast to class ReproducerKt$main$CustomResponse (org.http4k.core.MemoryResponse and ReproducerKt$main$CustomResponse are in unnamed module of loader 'app')
at ReproducerKt.main(Reproducer.kt:11)
I’m running it with Kotlin 2.0.0 and I was also able to reproduce it with 1.9.21 to ensure it’s not related to K2. http4k version is 5.21.1.0dave
06/06/2024, 12:21 PMwith
statement is taking MemoryResponse
as a generic and not Response
. You can fix it by upcasting to the Response
fun main() {
class CustomResponse : Response by Response(Status.OK)
val resp: Response = CustomResponse()
resp
.with(CONTENT_TYPE of ContentType.APPLICATION_JSON)
.let { println(it) }
}
dave
06/06/2024, 12:24 PMclass CustomResponse : Response by Response(Status.OK) {
fun with(vararg modifiers: (Response) -> Response) = (this as Response).with(*modifiers)
}
edrd
06/06/2024, 12:54 PMMemoryResponse
given Response.Companion.invoke
returns Response
.dave
06/06/2024, 1:04 PMedrd
06/06/2024, 1:07 PMedrd
06/06/2024, 1:07 PMdave
06/06/2024, 1:10 PMedrd
06/06/2024, 1:17 PMRequestContexts
and that looks like an alternative but I wanted to try my approach since it’s simpler.dave
06/06/2024, 1:18 PMedrd
06/06/2024, 1:18 PM