Subtle difference between Swift and Kotlin
There are lots of similarity between Swift and Kotlin, so we decided to use them for new cross-platform project in iOS and Android.
When we start the study of them, we soon noticed the subtle and important difference between them.
- Assignment can/cannot be used as condition
In swift the operation of assignment is an expression thus return true of false. Thus, we can use it as a condition.
In swift we often use this when unwrapping optional objects. The similar mechanism in Kotlin is Nullable. We can use it to achieve optional-like behavior. We are going to write this in another article.
- Swift let and Kotlin val
Swift let means the object is immutable. On the other hands, Kotlin val means the object is read-only. So, you can change the member variables of the instance which was created as val object 1. This difference is annoying.
Reflecting this Kotlin has special mutable collection like MutableSet (Set is read-only.)
We have to consider this difference at the design phase of the project.
- Programming Kotlin, Stephen Samuel, Stefan Bocutiu, 2017 Packt Publishing (p.30) ↩︎