https://kotlinlang.org logo
#getting-started
Title
# getting-started
l

LastExceed

04/04/2022, 11:28 AM
Copy code
val buffer = ByteBuffer.allocate(4450)
intellij says the inferred type of this is
val buffer: (ByteBuffer..ByteBuffer?)
what does that mean? this isn't even valid code when i try to type this out myself. the code compiles fine on my machine, but jitpack.io fails because of a type mismatch. what is going on here?
m

Michael de Kaste

04/04/2022, 11:38 AM
ByteBuffer allocation is probably Java Interrop, which is either nullable or not. You'll probably have to handle something like:
val buffer = ByteBuffer.allocate(4450)!!
or
val buffer = ByteBuffer.allocate(4450) ?: return someFailedReturn
can't the type just be ByteBuffer?
l

LastExceed

04/04/2022, 11:38 AM
shouldnt it be
ByteBuffer!
then ?
i updated to kotlin 1.6.20 which just released, and now it does show
ByteBuffer!
as expected. however jitpack still fails with
Type mismatch: inferred type is Buffer! but ByteBuffer was expected
i am so confused
s

Sam

04/04/2022, 11:57 AM
Inferred type is
Buffer
? That sounds like something from a different library entirely. Is that a typo or is that the exact wording of the error message?
l

LastExceed

04/04/2022, 12:00 PM
its copypasted from the log. i was wondering about this too
s

Sam

04/04/2022, 12:02 PM
Are you using any other libraries like ktor or okio?
l

LastExceed

04/04/2022, 12:06 PM
i am using ktor, but i already double checked that i'm not using the wrong type. its definitely
java.nio.ByteBuffer
everywhere
e

ephemient

04/04/2022, 12:11 PM
you are probably running into a Java version difference somewhere
e.g.
ByteBuffer.clear()
is a method of its
Buffer
supertype on Java 8- and so returns a
Buffer
, while there is a specific overload on Java 9+ that returns a
ByteBuffer
instead
👏 1
blob think smart 1
it's a pain, but unrelated to Kotlin and whatever you're seeing in your IDE
d

dmitriy.novozhilov

04/04/2022, 12:13 PM
T!
is a short form of
(T..T?)
type
l

LastExceed

04/04/2022, 12:17 PM
@ephemient ah yes that seems to be it. thanks! @dmitriy.novozhilov thats good to know, can you elaboraote or point me to a resource where i can learn more about this?
e

ephemient

04/04/2022, 12:21 PM
it's internal notation for lower and upper bounds, it's not something you can type yourself (well, not that you can type the platform type yourself either)
1
d

dmitriy.novozhilov

04/04/2022, 12:21 PM
l

LastExceed

04/04/2022, 12:22 PM
thanks
e

ephemient

04/04/2022, 12:23 PM
https://youtrack.jetbrains.com/issue/KTOR-1398 btw. there's supposed to be a
-Xjdk-release
coming in Kotlin 1.7, similar to Java's
--release
flag, to make it a bit less painful to target older JDKs when building with a newer one'
6 Views