Swift - Kotlin Syntax Comparation

Kurniadi
2 min readDec 10, 2020

I take new challenge on this end of the year to learn Android with Kotlin after more than 5 years experience as iOS Developer. I won’t say that both language is have high degree similarity. But i want to write some of the syntax and help mapping it to iOS knowledge, so hopefully will help iOS developer who want to try Android to have brief mind map between that two language. To make it easier please remember that the left side is Swift concept and the right side is Kotlin

I really appreciate any feedback if there are something wrong with my explanation or sample, since i just started learning Kotlin 2 weeks ago.

Var, Let — Var, Val

Both Var keyword in Swift and Kotlin have same concept as mutable property keyword. The difference come with immutable property. Swift use Let and Kotlin use Val. As iOS developer, it’s easier to remember the L in Val as a marker to L character in Swift Let keyword

Optional — Nullability

var name: String? in Swift vs var name: String? = null. Both code have same meaning to allow name property to null / nil (absent of value) or indicates that maybe this property is have possibility to be null / nil

Coalescing Operator — Elvis Operator

Both term have same meaning, if the left side of the operator is nil or null please use the right side of operator value as initialized value. Coalescing Operator is marked by “??” in other side Elvis Operator is marked as “?:”

Lazy — by lazy

Both have usage in Swift and Kotlin, to delay object instatiation until it’s first usage.

Implementation for Kotlin different because we put lazy at the back

_example will initialized when class instatiate, but example will not initialized until some operation access.

Static — Companion Object

In Swift, we put static as keyword to made a property can be accessible by their class/struct name.

For Kotlin, we must put property that we want to be accesible by class name defined and put inside companion object.

if we try to run it, it will have same result for both of them

Protocol — Interface

Both explanation is pretty simple and the usage is almost same withing Swift and Kotlin. They are `contract` that need to be fulfilled by whom implement them. Let put aside that protocol can have `optional` which make we can decide to not implement some of the function or property. They both can inherit another protocol. For keep it simple in mind, if you not using all of the functionality that Swift and Kotlin provide, they will look almost exactly the same in code implementation

To be continued

--

--