is there any documentation on using the kotlin `Li...
# kotlin-native
m
is there any documentation on using the kotlin
List
type (and other types from the
collections
package) in dynamic libraries? the generated headers indicate that a
<pkg>_kref_kotlin_collections_List
type is being used, but I can’t find any docs on it
d
Create an API in your dynamic library that you can use to create and manipulate them.
m
sorry, i don’t understand. if i wanted to write C code to add an item to a kotlin
List
, what would that look like? do I have to add boilerplate to my kotlin code to do so?
d
yes, you have to create a function in Kotlin that will do it.
A top-level one
m
do you have any example code? i can’t find almost any examples of using dynamic libraries
d
I do not, and I'm not an expert here so please don't take my words for granted.
r
https://kotlinlang.org/docs/tutorials/native/dynamic-libraries.html Here's general documentation on C API provided by K/N compiled into dynamic library. As you can see, all the methods should be available, namespaced by module name and package, taking object as the first argument.
Are stdlib classes covered in the same way in your generated headers?
m
is that a question? i have no idea. if i have a simple function that takes e.g.
List<Int>
, i don’t know how (in C) to instantiate the list to pass it in
the previous commenter suggested that i need to make top-level boilerplate functions to make a list, like
Copy code
fun make_int_list(): MutableList<Int> {
    return mutableListOf<Int>();
}
well, that appears to work for list operations. i guess i can build on that
d
I'm not certain but you might have to work with
StableRef
as well.
r
I mean, you can check the header, right? You see your own
make_int_list
there. I'm curious, is
mutableListOf
represented somehow or not?
d
That's an inlined function I believe. I would be curious as well.
You would want to find the
ArrayList
constructor and invoke it.
r
Right. It's also a generic over boxed primitive, which I have no idea how to work with via C code, as boxing isn't covered by the doc :(
m
ah, i see what you were saying. no, nothing other than my custom classes/functions are represented in the header file
so no
mutableListOf
, etc
the stdlib classes that are represented in func signatures get their own
struct
definitions, so i can dispose of them using the normal
pinned
pointer ref
o
List is no different from any other type fo reverse C interop, library must expose all required operations.
-Xexport-library
may be helpful to add single helper .klib to multiple dynamic libs.