Learning Kotlin and Swift

Kotlin vs Swift: The Extension

Explore Kotlin and Swift together and learn their differences

Photo by Toa Heftiba on Unsplash

In SOLID Principle, the second one is the Open-Close Principle, i.e. Open for Extension and Close for Modification. Both Kotlin and Swift do have the capability to extends to a class on top of its ability for object inheritance.

The extension

Even though both Kotlin and Swift have extensions, they are different.

Kotlin has Function Extension

class MyClass { }
fun MyClass.extensionFunction() { }

Swift have Class Extension

class MyClass { }
extension MyClass {
func extensionFunction() { }
}

While both may look similar, but they do differ in capabilities. Below I’m sharing the advantage of each.

The function extension

In Kotlin, only have function extension support. On top of just extending a function to a class, below are it’s advantage compare to class extension

Nullable receiver extension function

Other than extending a function on a normal class, the extension function in Kotlin can also extend…

--

--