https://kotlinlang.org logo
#touchlab-tools
Title
# touchlab-tools
m

mattinger

11/29/2023, 2:38 PM
Hi all, I’ve been playing around with the SKIE gradle plugins, and I encountered this wording:
Copy code
Going back up in the code, we mentioned that lookupOperationType needed an explicit defaultValue because default arguments are not available to Swift.
I’m a little confused by this, because swift does indeed support default parameter values. I’m not sure when that was added and if this is a limitation due to what version of swift that SKIE is targeting, or some other limitation. So, I then tried to use the annotations:
Copy code
@DefaultArgumentInterop.Enabled
@DefaultArgumentInterop.MaximumDefaultArgumentCount(25)
Unfortunately, we have some very large data classes with a really huge number of constructor parameters. Even at this number, the process kept running out of heap space (i gave it 4g of memory) trying to build. I’m curious what SKIE is doing under the covers (it is doing something like what @JvmOverloads does), or is it smart enough to generate swift functions with default values?
f

Filip Dolník

11/29/2023, 2:52 PM
Hi! The problem the docs mentions is that Swift doesn’t support default arguments specifically for Kotlin functions. The reason why is that these functions are exported as Obj-C functions and Obj-C does not support default arguments. Additionally, Swift default arguments are not semantically equivalent to Kotlin default arguments. (Not everything that you can do in Kotlin can be done in Swift. For example the copy method of data classes cannot be written in Swift) SKIE has a very primitive solution for this - it just generates overloads for all combinations of the default arguments. The reason why there is the default limit of 5 and the default arguments are opt in is that the number of generated overloads is exponential. So in your case of 25, SKIE would generate 2^25 functions, so about 33 million
m

mattinger

11/29/2023, 3:13 PM
Thanks. That definitely answers the question. Out of curiousity, is it possible to generate a swift class that’s more similar to the kotlin class in terms of default arguments and the like (i get the copy function thing though). I know it would technically be a new class, but this happens for enums when you enable that feature.
Or is this just something we kind of have to wait for Jetbrains to finish their work on swift exports (which they claim is a 2024 priority)
f

Filip Dolník

11/29/2023, 3:19 PM
generating the class is not necessary, you can create a Swift extension function for most Obj-C classes without issues and those can have default arguments (but with the limitations mentioned above) We actually have some internal prototype of that. And it might eventually get to SKIE but don’t know when.
As for the Swift interop - not much is known about this at this point so it’s not possible to say. However, I’d assume that default arguments will be eventually supported at least in some limited form. But that’s far in the future. (It’s their 2024 priority but that means they will work on it - nothing about when it will be ready, and I can tell you that SKIE does fraction of what needs to be solved for the full export and it took insane amount of time 😄 because this is quite a difficult problem to solve.)
10 Views