Skip to main content

Global Functions

Overview

To minimize name collisions, Kotlin exposes global functions under a namespace. Objective-C doesn't have a proper concept of namespaces, so this namespace is implemented as a class with static members. This namespace's name is derived from the file name in which the function is defined.

This approach works but is not ideal because it unnecessarily complicates the syntax. It requires the user to know the name of the Kotlin file in which the function is defined. And if there are multiple files with the same name in different modules, they create a new type of collision.

Because SKIE has much better handling of overloaded functions, those collisions are less likely to happen. As a result, using the namespace syntax no longer provides any value in most cases. So, to improve the syntax, SKIE generates actual global functions for these namespaced global functions.

tip

Even though this page only mentions global functions, the same applies to global properties - those are also supported.

Example

Let's say we have a Kotlin file named File.kt with the following global function:

Kotlin (File.kt)
fun globalFunction(i: Int): Int = i

This function has to be called from Swift like this:

Swift without SKIE
FileKt.globalFunction(i: 1)

Using the generated wrapper, the same function can be called simply:

Swift with SKIE
globalFunction(i: 1)

Limitations

The only known limitation has to do with the still possible name collisions. If such a name collision occurs, the generated wrapper will be renamed using the standard "_" suffix. However, the original function will still be available under its original name (assuming it is unique in its namespace).

caution

We do not recommend using any declarations that are renamed using the "_" suffix in Swift. Read more about the associated risks here.

Migration and Compatibility

This feature shouldn't introduce any breaking changes. The original functions are unmodified and still available under their original names. Therefore, incremental adoption of this feature is possible without any manual configuration.