I'm having an issue with KotlinPoet and I can't se...
# squarelibraries
v
I'm having an issue with KotlinPoet and I can't seem to figure it out. I'm trying to build a statement that uses a number of extension functions and the problem is with the
alloc
extension function. On some classes it manages to "find" and "import" the alloc extension fine, while on other classes it does not, and it generates something like
Box(nativeHeap.kotlinx.cinterop.alloc<,,,>().ptr
. Any idea what could be the reason for the member not being recognized in some classes but recognized in others? Any tips on debugging this?
The wrong one generates this (without the import statement):
Copy code
public fun allocateHeap(): Box =
                Box(nativeHeap.kotlinx.cinterop.alloc<graphene_box_t>().ptr)
and when I manually fix the code to this while also in IntelliJ
<Alt>Enter
on alloc to add the import, it works
Copy code
public fun allocateHeap(): Box =
                Box(nativeHeap.alloc<graphene_box_t>().ptr)
e
https://square.github.io/kotlinpoet/#:~:text=addAliasedImport
You just need to make sure the member can be imported without simple name collisions, otherwise importing will fail and the code generator output will not pass compilation.
v
hmm, I just discovered that the classes that have problems with this also have a generated
alloc
function in the same companion object as the one I am generating my problematic function in. So it looks like KP is not importing the extension member when it finds a similarly named member in the same Spec, am I doing something wrong by using
MemberName
for extension functions?
e
multiple things named
alloc
require manual disambiguation
v
I'm giving the aliased import a try, that will probably solve my issue