How do types like `NSArrayAsKList` work? Are we ba...
# kotlin-native
j
How do types like
NSArrayAsKList
work? Are we basically just reinterpreting the pointer to the
NSArray
as one of these instances on the C-side?
I did
Copy code
interpretObjCPointer<NSMutableArrayAsKMutableList>(NSMutableArray().objcPtr()) as MutableList<E>
and it seemed to work
Now I'm wondering why Darwin platforms don't typealias ArrayList to this type and create an NSMutableArray instead of having a dedicated implementation of ArrayList
s
Are we basically just reinterpreting the pointer to the
NSArray
as one of these instances on the C-side?
Not quite.
NSArrayAsKList
is a proxy class. Its instances are created as “associated objects” when
NSArray
is “crossing border” between Objective-C and Kotlin worlds.
Copy code
interpretObjCPointer<NSMutableArrayAsKMutableList>(NSMutableArray().objcPtr()) as MutableList<E>
So in this example we are wrapping/unwrapping Objective-C object several times.
Now I’m wondering why Darwin platforms don’t typealias ArrayList to this type and create an NSMutableArray instead of having a dedicated implementation of ArrayList
There are lots of reasons. To name a few: • We need to keep consistent behaviour between different targets. • As shown above, there is an overhead from interaction between 2 runtimes. • We don’t depend on whatever Apple will do to
NSMutableArray
.
j
If you can't depend on Apple then how can you transparently expose an NSMutableArray as a MutableList? That would mean some lists will behave differently than others. So either you can rely on them and transparently wrap their type, or you can't rely on them at all and need to do a full wrapper class.
s
These are quite different things. Exposing NSMutableArray as MutableList is fine as long as its public API allows that. Delegating implementation of ArrayList to NSMutableArray is on absolutely different level. Like, we need to control performance of the implementation.