svtk / kotlin-swift-interopedia

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Kotlin-Swift interopedia

Common

Internal modifierInternal functions and classes are not visible in Swift.
JavaDoc commentsSome KDoc comments are visible if you add a special compiler argument.
Exceptions and errorsExceptions declared with @Throws are represented with NSError and must be handled. Not declared exceptions will cause a program termination.

Data types

Collections with custom types dataCollections with elements of custom types do not require additional mappings.
Collections with primitive typesCollections with elements of primitive types (except String) require a wrapper.
Primitive typesMay require mapping for integer data types and mapping for Char.
Optional (nullable) primitive typesSome primitive types require mapping into special optional types.
Mutable, immutable collectionsTo adjust mutability, the let and var keywords are used. Additional mappings are required for mutable collections.
Unit and NothingThe Unit and Nothing types can be used in the same way as in Kotlin: Unit as an object or void, Nothing cannot be created.

Usual workflow

Companion objectAccess to functions and properties is available through the companion auxiliary object.
Constructor with default argumentsYou always have to specify all the arguments for a constructor. Improved interop available with SKIE.
Extension function over platform classA wrapper class appears with a method that accepts an object of the desired class.
Extension properties for companion object of platform classThere is a property in the .h file, but in Swift it’s impossible to use.
Extension properties for companion object of usual classThe property can be accessed through the companion object.
Extension properties over platform classA wrapper class appears with a function that accepts an object of the desired class.
Extension properties over usual classThe property can be accessed through the class object.
Function with default argumentsYou always have to specify all the function arguments. Improved interop available with SKIE.
ObjectsAccess is available through the shared auxiliary object.
Top-level functionsA wrapper class appears: TopLevelFunctionKt.topLevelFunction().
Top-level val properties (readonly)A wrapper class for accessing the property appears: TopLevelPropertyKt.topLevelProperty.
Top-level var properties (mutable)A wrapper class for accessing the property appears: TopLevelPropertyMutableKt.topLevelPropertyMutable.
Usual class constructorWorks the same as Kotlin.
Usual class functionWorks the same as Kotlin.
Usual class val property (readonly)The property is accessible from the class object and is a readonly property.
Usual class var property (mutable)The property is accessible from the class object and is a mutable property.

Classes

Abstract classIDE has no hints to override abstract methods, rather we get a crash when trying to use the method during runtime.
Annotation classAnnotations were not included in the .h file.
Data classNot all features of data classes are preserved, some are renamed. Improved interop available with SKIE.
Enum classDoesn't work as expected. But an object with static elements was generated. Improved interop available with SKIE.
Inner classMinor differences in creation syntax.
Open classCan inherit from open class, use its protected properties, override open, but not override final methods.
Sealed classA class with heirs is generated. Passing to a switch statement requires a default case. Improved interop available with SKIE.
Inline classThe class was not included in the .h file and cannot be used.

Interfaces

Fun interfaceYou can't create an anonymous class in Swift.
InterfaceThe interface has become @protocol. But for some reason the val property turned into var.
Sealed interfaceSeparate protocols were generated that were not related to each other.

Functions

DSLFunctions with receivers have been generated, it doesn’t look as convenient as we would like.
Function returns lambdaThe function that returned the lambda works without crashes; lambda can be called.
Function returns primitive typeA function returning a primitive type works without error.
Function with extension function as argsThe extension function turns into a lambda with a parameter.
Function with lambda argumentsA function that takes one or more lambdas as arguments converts normally in Swift.
Function with no return typeFunctions that return nothing can be safely called.
Function with value class parameterThe function appeared in the .h file, but the value class argument was turned into primitives.
Function with vararg parametervarargs are not mapped to Swift's variardic parameters, but instead to KotlinArray.
Functions with overloadsThere are some peculiarities when using the same parameter names.
Inline functionsInline functions are in the .h file, they can be called.

Generics

Bounded genericsThe generic type restriction doesn't work.
Contravariant genericsDoesn't work as expected, you have to use a type cast.
Covariant genericsDoesn't work as expected, you have to use a type cast.
Generic classesThere are some features of using types.
Generic functionsNo automatic type inference, nullability features.
Generic interfacesGeneric interfaces are not supported.
Reified functionsThe reified function crashes at runtime.
Star projectionDoesn't work as expected, you have to use a type cast.

Coroutines

Suspend functionsTranslated into callback, experimentally - into async / await. Libraries like SKIE and KMP-NativeCoroutines can be used to improve the interop and provide cancellation support.
FlowsTranslated into callback, experimentally - into async / await. Generic type arguments are lost. Libraries like SKIE and KMP-NativeCoroutines can be used to improve the interop and provide cancellation support.

About

License:MIT License


Languages

Language:Swift 65.4%Language:Kotlin 34.6%