# Main difference between Extensions on Swift and Kotlin

**URL:** <https://forums.kodeco.com/t/main-difference-between-extensions-on-swift-and-kotlin/51559>\
**Category:** Android\
**Created:** [October 2, 2018, 10:43am UTC](https://forums.kodeco.com/t/main-difference-between-extensions-on-swift-and-kotlin/51559 "2018-10-02T10:43:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![amadeu01](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/amadeu01/32/110248_2.png) [@amadeu01](https://forums.kodeco.com/u/amadeu01)\
**Post date:** [October 2, 2018, 10:43am UTC](https://forums.kodeco.com/t/main-difference-between-extensions-on-swift-and-kotlin/51559/1 "2018-10-02T10:43:29Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![amadeu01](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/amadeu01/32/110248_2.png) [@amadeu01](https://forums.kodeco.com/u/amadeu01)\
**Post date:** [October 2, 2018, 4:06pm UTC](https://forums.kodeco.com/t/main-difference-between-extensions-on-swift-and-kotlin/51559/2 "2018-10-02T16:06:23Z")

</div>

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

```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

```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
    }
}

```

---

<div class="post-metadata">

**Author:** ![jayantvarma](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jayantvarma/32/111768_2.png) [@jayantvarma](https://forums.kodeco.com/u/jayantvarma)\
**Post date:** [October 28, 2018, 5:58am UTC](https://forums.kodeco.com/t/main-difference-between-extensions-on-swift-and-kotlin/51559/3 "2018-10-28T05:58:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![amadeu01](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/amadeu01/32/110248_2.png) [@amadeu01](https://forums.kodeco.com/u/amadeu01)\
**Post date:** [October 31, 2018, 2:18am UTC](https://forums.kodeco.com/t/main-difference-between-extensions-on-swift-and-kotlin/51559/4 "2018-10-31T02:18:37Z")

</div>

hi @jayantvarma  
Sorry for my delay answer.

Actually

This Code

```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
}

```

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

![image](https://files.chunter.kodeco.com/original/3X/8/b/8bbb23baef683262a3c3ea9b80e70133bf2e1e21.jpeg)

---

<div class="post-metadata">

**Author:** ![jayantvarma](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jayantvarma/32/111768_2.png) [@jayantvarma](https://forums.kodeco.com/u/jayantvarma)\
**Post date:** [October 31, 2018, 6:52am UTC](https://forums.kodeco.com/t/main-difference-between-extensions-on-swift-and-kotlin/51559/5 "2018-10-31T06:52:28Z")

</div>

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) [Access Control — The Swift Programming Language (Swift 5.7)](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

---

<div class="post-metadata">

**Author:** ![amadeu01](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/amadeu01/32/110248_2.png) [@amadeu01](https://forums.kodeco.com/u/amadeu01)\
**Post date:** [October 31, 2018, 12:37pm UTC](https://forums.kodeco.com/t/main-difference-between-extensions-on-swift-and-kotlin/51559/6 "2018-10-31T12:37:16Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [March 18, 2019, 2:43am UTC](https://forums.kodeco.com/t/main-difference-between-extensions-on-swift-and-kotlin/51559/7 "2019-03-18T02:43:34Z")

</div>

Closed on March 18, 2019, 2:43am UTC

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