Main difference between Extensions on Swift and Kotlin

Hi,

I do not know if here is the best place to ask about kotlin and swift.

So, I have been searching about the differences between Kotlin and Swift. And, I did not understand what extensions in Swift can do that in Kotlin you couldn’t do and vice-versa.

I know that in kotlin, extensions are static functions that receive the called class as parameter. But, what makes it different from swift? In swift, can we access private fields of the class using extensions and in kotlin we cannot ?

I believe that the greatest difference is that on Kotlin, the extensions are much more like a helper and a syntax reducer than a magic thing like in swift.

In Kotlin

class Year(val year: Int) {
    private val isLeap: Boolean

    init {
        isLeap = isYearLeap()
    }

    private fun isYearLeap() : Boolean {
        return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)
    }
}

fun Year.getYear() : Boolean {
    return this.isLeap
}

In Swift

class Year {
    private let isLeap: Bool
    private let year: Int
    init(year: Int) {
        self.year = year
        self.isLeap = isYearLeap()
    }
    
    private func isYearLeap() -> Bool {
        return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)
    }
}

extension Year {
    func getYear() -> Bool {
        return self.isLeap
    }
}

Hi @amadeu01,
hope you have found an answer to your own question.

I had a question, from the examples both the kotlin and the swift code do the same thing and are called in the same way, then how is one a helper and the other magic?

cheers,

Jayant

hi @jayantvarma
Sorry for my delay answer.

Actually

This Code

class Year(val year: Int) {
    private val isLeap: Boolean

    init {
        isLeap = isYearLeap()
    }

    private fun isYearLeap() : Boolean {
        return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)
    }
}

fun Year.getYear() : Boolean {
    return this.isLeap
}

On Kotlin will not compile. Once the extension cannot access the internal field isLeap

Hi @amadeu01,
Thanks for sharing that snippet, the problem is not magic, it is the access levels. You might want to read up on them (Swift) https://docs.swift.org/swift-book/LanguageGuide/AccessControl.html

This is both an advantage as well as a disadvantage depending on how you look at it.

cheers,

Jayant

hi @jayantvarma
Sorry so my language. And, thanks for your response!
When I said helper I meant this:

We would like to emphasize that extension functions are dispatched statically , i.e. they are not virtual by receiver type. This means that the extension function being called is determined by the type of the expression on which the function is invoked, not by the type of the result of evaluating that expression at runtime.
https://kotlinlang.org/docs/reference/extensions.html

Kotlin extensions are like static methods on Java.

On the other hand, in swift:

Extensions add new functionality to an existing class, structure, enumeration, or protocol type. This includes the ability to extend types for which you do not have access to the original source code (known as retroactive modeling ). Extensions are similar to categories in Objective-C. (Unlike Objective-C categories, Swift extensions do not have names.)

It seems that in swift the new methods or new properties are added to the class, structure, etc.

This topic was automatically closed after 166 days. New replies are no longer allowed.