DEV Community

ScienceSoft
ScienceSoft

Posted on • Updated on

Kotlin vs. Swift: Are Android and iOS moving towards creating a universal language?

alt text

Once Kotlin received Google’s support, the sheer excitement of Kotlin fans wasn’t the only reaction to follow. Those not familiar with Kotlin were quite concerned about its compatibility/interoperability level with Java, the average time necessary to get the hang of it, as well as the advantages of using Kotlin in general.

In their attempts to explain and introduce the language in the shortest and clearest way possible, many Kotlin developers referred to a 3-year-old parallel between Kotlin and the second official language of iOS – Swift. Calling Kotlin “the Swift of Android” did make things easier and helped to create an image for the language. Yet this image also provoked arguments in the iOS community, as some iOS developers didn’t find the comparison flattering and saw Kotlin as a mere copycat.

At the very least, it should be noted that while Swift appeared in 2013, Kotlin originated back in 2011. Hence, even though comparing Kotlin to Swift (in this exact order) can be convenient due to Swift’s earlier introduction to a wide audience, any ‘copycat’ attitudes towards Kotlin aren’t justified.
Still, does the comparison stand? If yes, how far does the similarity stretch? And does its existence hint at the fact that delivering apps for both iOS and Android natively can become easier and faster in the future? ScienceSoft's huge experience in mobile app development services allows to speculate at this point. Let’s look into it.

Syntax

The syntax of Swift doesn’t just resemble that of Kotlin: in small chunks of code there can be up to 77% string similarity. Major differences can be reduced to the table below:

Kotlin Swift
fun func
val let
null nil
trait protocol
constructor init
: ->
Any AnyObject
!! !

Basics, classes and functions all have very similar ways of expression. Unlike Objective-C’s, Swift’s method calls are similar to those of Java and Kotlin, with their namespace system and dot-notation style. For example, here’s what function call looks like in the two languages:

Kotlin Swift
fun forecast(day: String, weather: String): String { func forecast(_ day: String, _ weather: String) -> String {
return "Today is $day, it's $weather." return "Today is \(day), it's \(weather)."
} }
forecast("Monday", "Raining") forecast("Monday", "Raining")

And this is how classes are declared in both:

Kotlin Swift
class Residence { class Residence {
var numberOfRooms = 0 var numberOfRooms = 0
fun Description() = func Description() -> String {
"A house with $numberOfRooms." return "A house with \(numberOfRooms)."
} }
}

Many other examples can be found in this article, and if they tell us something, it is that both languages share the initial purpose of staying as concise and transparent as possible, making lives of developers easier. Kotlin’s and Swift’s syntax systems are quite effective in that regard, as they are appreciated by development teams for their elegancy.

Security

Although both Swift and Kotlin are strong and static in terms of typing, they also allow work with dynamic types. This way, the languages stay concise and flexible, while allowing early elimination of bugs and mismatches. Therefore, they are considered highly secure and especially reliable for big projects.
Apart from that, the two languages combine approaches to handling optional values and null/nil safety with either Safe Navigation Operator ? or Option types. The ? precaution is expressed in almost the same manner in both Kotlin and in Swift:

Kotlin Swift
val example: String? = null var example: String? = nil

Features

Besides null (nil) safety, functions and classes, Kotlin and Swift have multiple similar features, including constants, variables, generics, protocols (traits), enumerated types, any (anyobject), error handling and others. Some of the features implemented in the two languages share the approach, but are called differently due to the original language these features go back to.

For instance, in Kotlin one can find Java’s lambda expressions (which, by the way, are highly effective but aren’t available for Java Android development). In Swift these are blocks or closures, the terms from Objective-C. The way both expressions are called into code is as similar, as the way they work.

Kotlin Swift
{ { _in
println("Lambda Expression") print("Closure Expression")
} }

The feature known as computed properties in Swift, which is a specific property declaration with a ‘get’ call, is enabled in Kotlin as well:

Kotlin Swift
class Animal( class Animal {
var Genus: String, var Genus : String
var Species: String) { var Species : String
val binomialName: String var binomialName: String {
get() = "$Genus $Species" get {
} return "\(Genus) \(Species)"
}
}
}

Name parameters (or named arguments) are also used in both languages:

Kotlin Swift
fun daysoff(vacation: Int, weekends: Int): Int = vacation + weekends func daysoff(vacation: Int, weekends: Int) -> Int {
return vacation + weekends
}
daysoff(5, weekends = 8) daysoff(vacation: 5, weekends: 8)

In fact, instead of listing the features that exist in both languages, it would be easier to list those that don’t. Namely, only Kotlin supports:

  • class import,
  • primary constructors and data classes,
  • @annotations.

At the same time, unlike Kotlin, Swift has:

  • tuples,
  • typealias,
  • guard statement.

Meaning behind similarities

The two languages clearly share the ideology, as they solve the same problems created by their ancestor languages: they are less verbose and limited in functions, more readable and convenient to work with. At the same time, both Kotlin and Swift stay interoperable with Java and Objective-C respectively, which allows using them in new projects as well as in maintenance of old ones.

What’s more, the strong resemblance of the two languages can aid in native development of one app for both iOS and Android. This isn’t to say the apps on both platforms can share one code, of course, since the languages and OS-specific libraries aren’t identical. Still, the approaches to application logic and functionality can be very similar, thanks to syntactical and functional similarity between Swift and Kotlin. This can make development, testing and maintenance faster and easier.

Universal language for iOS and Android?

In theory, Google could have already accepted Swift as its official language instead of Kotlin; there were even rumors surrounding this possibility back in 2016. Such a move might have not created a situation where any cross-platform development tools became irrelevant, but the margin between the two platforms would have certainly become blurry.

However, such a step would have also been unreasonable, and not just because of business competitiveness. Although Swift and Kotlin are similar, what they resemble most are their predecessors. In other words, Swift and Kotlin are bridging the gap between Objective-C and Java. Yet a shift from Java to Kotlin is still more natural and smooth than that from Java to Swift.

In general, the thought of adjusting to something new doesn’t appeal to everyone; some developers take their time to give a new language a go, just like it was with the adoption of Swift. To make sure the transition would be less of an ordeal means ensuring the language will eventually catch on, and for a new language it is the first and foremost.

Parting thought

As mobile development constantly evolves, so does technology. That’s why in 5-10 years both Kotlin and Swift can become something entirely different. There is no telling whether the languages will continue to bridge the gap between each other. Still, as both iOS and Android are searching for the most convenient, secure and fast mobile development tool, they just might end up speaking the same language one day.

Top comments (49)

Collapse
 
tsikes profile image
tsikes

A few other points:

Both languages have a non-mobile aspect. Swift is also used for macOS development, and is available on Linux. IBM is promoting it for web server-side development. Kotlin was originally developed as a general purpose JVM language, and is also used for web server-side development.

Only Swift is compiled to native code. I realize there's a native Kotlin effort, but it's in its infancy at this point. Swift is highly competitive with C++ as far as runtime efficiency goes - and it should continue to improve.

Swift is reference counted instead of garbage collected. Kotlin is garbage collected. The practical result of this is that Swift doesn't suffer from GC latency, while Kotlin efficiently handles cycles in allocated objects. Apple (and I) both believe that GC latency is detrimental in desktop apps, games and any type of realtime programming.

I'm hopeful that both languages will continue to improve. It's unfortunate in my view that Google hasn't committed to Swift as its eventual preferred language for Android development. Swift versus Kotlin is another waste of developer mental bandwidth along the lines of Vulkan versus Metal versus Direct3D.

Collapse
 
dimpiax profile image
Dmytro Pylypenko

Kotlin is pathetic parody to Swift. Also Kotlin's syntax sugar is under doubt.

Related to article: Any in Kotlin is a base class?
Swift has Any datatype for func / protocol / enum etc. What Kotlin does have accordingly?

Collapse
 
lovis profile image
Lovis • Edited

Also Kotlin's syntax sugar is under doubt.

Who doubts what? Don't really understand what that's supposed to mean.

Related to article: Any in Kotlin is a base class?

Yes, Any is basically Object from Java

Swift has Any datatype for func / protocol / enum etc. What Kotlin does have accordingly?

Don't really get what you mean again, but if you want to know about the Kotlin type system, you can check this: natpryce.com/articles/000818.html
In short: Everything in Kotlin extends Any, also interfaces, enums and function types.

All in all it's worth noting that calling a language "pathetic parody" of another language is not constructive and does not get us anywhere. Actually, it bespeaks of a pretty childish attitude and most of the dev.to community prefers to have a civilized discussion instead of ranting.

In the end, some things are better in Swift and some are better in Kotlin. Many things are the same.

Collapse
 
aaronbond profile image
Aaron Bond

Kotlin came first dude. If anything swift is a pathetic copy of kotlin. :)

Collapse
 
dimpiax profile image
Dmytro Pylypenko

Wikipedia says this to you?
Also, decision about who came first is incorrect to statement about parody.

Aaron, adhere to ethics. "Dude" – keep it to yourself.

Thread Thread
 
aaronbond profile image
Aaron Bond

the definition of parody is to imitate in a humorous way, so which came first is pertinent to whether or not something can be a "pathetic parody" of something else.

and what am i supposed to keep to myself?

Thread Thread
 
nobody1707 profile image
Nobody1707

But in point of fact, Chris Lattner first started creating Swift in 2011, it simply wasn't released until 2014. They're similar solely because they have a similar set of influences. Neither is based on the other.

Collapse
 
technoplato profile image
Michael Lustig - halfjew22@gmail.com

Now now children. Everyone can have a seat at the table.

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
crummy profile image
Malcolm Crum

I think kotlin does support typealias as of 1.1.

Collapse
 
epabst profile image
Eric Pabst

True.

Collapse
 
andy profile image
Andy Zhao (he/him)

Thanks for the article. It definitely enlightened my view on the two mobile languages. I think for competitive reasons they won't ever merge, but it's interesting that they're similar in so many ways.

Collapse
 
mradzinski profile image
Matias Radzinski

"traits" is no longer a thing in Kotlin. It got replaced by "interface" like... two years ago? Honestly, I stopped reading at that.

blog.jetbrains.com/kotlin/2015/04/...

Collapse
 
bizzibody profile image
Ian bradbury

There will always be at least two types of reaction to articles like this.

[a] Horror that you promoted one or the other - or both
[b] Argument in the comments that you failed to mention x or y

Considering the difficulty of writing this type of comparison - I salute you.

Collapse
 
datadataservic1 profile image
Data Data Services

Comparing Kotlin with Swift. Can these languages trigger the appearance of a universal mobile development language for both Android and IOS. visit: compunneldigital.com/digital-trans...

Collapse
 
acicartagena profile image
a c

nice write up. i noticed a few similarities when my coworker was presenting kotlin. (i use swift)

just a small correction though, you're missinv the '\' in the string interpolation. it should be "hi (name)" instead of "hi (name)"

Collapse
 
sahil_kwatra profile image
sahil kwatra

Great Article and well informed. I always love to read about technology. Here They Explain Well About Computer Se Mobile Me File Transfer Kaise Kare

Collapse
 
alliancetekinc profile image
AllianceTek

I agree with you that some points are very similar in both languages Kotlin and Swift. But both are used by developers according to their requirements. After getting Google support from 2017 Kotlin becomes more popular then Swift. Review link that how both are different in many things - bit.ly/2HztGRY

Collapse
 
newshubfeed1 profile image
NewsHubFeed

Great Article and well informed I always love to read about technology and pets.Here They Explain Well About How often to change cat litter

Collapse
 
newshubfeed1 profile image
NewsHubFeed

Wikipedia says this to you?
Also, decision about who came first is incorrect to statement about parody.

Aaron, adhere to ethics. "Dude" – keep it to yourself.
Newshubfeed

Collapse
 
arunprakash142 profile image
Arunprakash142

wow... this a feel good article if any of the business person are looking for assistance regarding there software development company. I found this site software development company.. young minds technology solutions is the company name for more details visit their site.

Collapse
 
kakashidinho profile image
Le Hoang Quyen

They already have an universal language called C.

Collapse
 
palle profile image
Palle • Edited

A big difference are value types, which are extremely important in Swift.

Collapse
 
lovis profile image
Lovis

yes. I would really love to have them for Kotlin, but the JVM does not (yet) support value types.

Collapse
 
epabst profile image
Eric Pabst

Swift value types are really a collection of language features. Here is a comparison of Swift's value types with Kotlin's features:

Enums representing choice

Swift:
enum VideoQuality {
case High
case Medium
case Low
}

Kotlin:
enum VideoQuality { High, Medium, Low }

Enums representing different things

Swift:
enum VideoMetadata {
case Program(channelId: String, programId: String)
case Ad(channelId: String, adId: String)
case Filler(channelId: String)
}

Kotlin:
sealed class VideoMetaData
data class Program(val channelId: String, val programId: String) : VideoMetaData()
data class Ad(val channelId: String, val adId: String) : VideoMetaData()
data class Filler(val channelId: String): VideoMetaData()

Tuples

Swift:
typealias Person = (name: String, age: Int)
let person = (name: "George", age: 30)
let (name, age) = person

Kotlin:
data class Person(val name: String, val age: Int)
val person = Person("George", 30)
val (name, age) = person

Note: I'm not aware that Kotlin supports passing a data class to a function in place of multiple named parameters. The workaround of course is to take the class as a parameter.

Struct

In Kotlin, a Swift struct would be ported to be a data class or basic class. However, it is NOT copied when passed around. However, there is an explicit copy method on Kotlin data classes to allow modifying on copy. (FWIW, I don't think I'd like an implied copy on assignment in Kotlin anyway.)

"Primitives"

Swift: Int, Bool, Double
Kotlin: Int, Boolean, Double

Class

They're basically the same in Kotlin as explained above.

Collapse
 
palle profile image
Palle • Edited

You could just call Swift code from Kotlin using the JNI :)

Thread Thread
 
lovis profile image
Lovis

Haha, "just" 😅

Some comments may only be visible to logged-in visitors. Sign in to view all comments.