DEV Community

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

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
 
palle profile image
Palle • Edited

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

Thread Thread
 
lovis profile image
Lovis

Haha, "just" 😅

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.