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

November 17, 2018 0 Comments

Syntaxt Differ : 

Swift and Kotlin are two great languages for iOS and Android development respectively. They both have very modern features and syntax that can help enormously to build native apps.
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.
Hello World
Swift:
print("Hello, world!")
Kotlin:
println("Hello, world!")
Variables And Constants
Swift:
var myVariable = 42
myVariable = 50
let myConstant = 42
Kotlin:var myVariable = 42
myVariable = 50
val myConstant = 42
Explicit Types
Swift:
let explicitDouble: Double = 70
Kotlin:
val explicitDouble: Double = 70.0
Type Conversion
Swift:let label = "The width is "
let width = 94
let widthLabel = label + String(width)
Kotlin:
val label = "The width is "
val width = 94
val widthLabel = label + width 
String Interpolation
Swift:
let apples = 3
let oranges = 5
let fruitSummary = "I have \(apples + oranges) " +
                   "pieces of fruit."
Kotlin:
val apples = 3
val oranges = 5
val fruitSummary = "I have ${apples + oranges} " +
                   "pieces of fruit."
Range Operator
Swift:let names = ["Anna", "Alex", "Brian", "Jack"]
let count = names.count
for i in 0..<count {
    print("Person \(i + 1) is called \(names[i])")
}
// Person 1 is called Anna
// Person 2 is called Alex
// Person 3 is called Brian
// Person 4 is called Jack
Kotlin:val names = arrayOf("Anna", "Alex", "Brian", "Jack")
val count = names.count()
for (i in 0..count - 1) {
    println("Person ${i + 1} is called ${names[i]}")
}
// Person 1 is called Anna
// Person 2 is called Alex
// Person 3 is called Brian
// Person 4 is called Jack
Collections(Arrays)
Swift:var shoppingList = ["catfish", "water",
    "tulips", "blue paint"]
shoppingList[1] = "bottle of water"
Kotlin:val shoppingList = arrayOf("catfish", "water",
    "tulips", "blue paint")
shoppingList[1] = "bottle of water"
Empty Collections
Swift:let emptyArray = [String]()
let emptyDictionary = [String: Float]()
Kotlin:val emptyArray = arrayOf<String>()
val emptyMap = mapOf<String, Float>()
Functions
Swift:func greet(_ name: String,_ day: String) -> String {
    return "Hello \(name), today is \(day)."
}
greet("Bob", "Tuesday")
Kotlin:fun greet(name: String, day: String): String {
    return "Hello $name, today is $day."
}
greet("Bob", "Tuesday")
Tuple Return
Swift:func getGasPrices() -> (Double, Double, Double) {
    return (3.59, 3.69, 3.79)
}
Kotlin:
data class GasPrices(val a: Double, val b: Double,
     val c: Double)
fun getGasPrices() = GasPrices(3.59, 3.69, 3.79)
Higher Order function (Map)
Swift:let numbers = [20, 19, 7, 12]
numbers.map { 3 * $0 }
Kotlin:val numbers = listOf(20, 19, 7, 12)
numbers.map { 3 * it }
Sort
Swift:var mutableArray = [1, 5, 3, 12, 2]
mutableArray.sort()
Kotlin:listOf(1, 5, 3, 12, 2).sorted()
Classes
Swift:class Shape {
    var numberOfSides = 0
    func simpleDescription() -> String {
        return "A shape with \(numberOfSides) sides."
    }
}
var shape = Shape()
shape.numberOfSides = 7
var shapeDescription = shape.simpleDescription()
Kotlin:class Shape {
    var numberOfSides = 0
    fun simpleDescription() =
        "A shape with $numberOfSides sides."
}
var shape = Shape()
shape.numberOfSides = 7
var shapeDescription = shape.simpleDescription()
Protocol
Swift:protocol Nameable {
    func name() -> String
}
func f<T: Nameable>(x: T) {
    print("Name is " + x.name())
}

Kotlin:interface Nameable {
    fun name(): String
}
fun f<T: Nameable>(x: T) {
    println("Name is " + x.name())
}
Extensions
Swift:extension Double {
    var km: Double { return self * 1_000.0 }
    var m: Double { return self }
    var cm: Double { return self / 100.0 }
    var mm: Double { return self / 1_000.0 }
    var ft: Double { return self / 3.28084 }
}
let oneInch = 25.4.mm
print("One inch is \(oneInch) meters")
// prints "One inch is 0.0254 meters"
let threeFeet = 3.ft
print("Three feet is \(threeFeet) meters")
// prints "Three feet is 0.914399970739201 meters"

Kotlin:val Double.km: Double get() = this * 1000
val Double.m: Double get() = this
val Double.cm: Double get() = this / 100
val Double.mm: Double get() = this / 1000
val Double.ft: Double get() = this / 3.28084
val oneInch = 25.4.mm
println("One inch is $oneInch meters")
// prints "One inch is 0.0254 meters"
val threeFeet = 3.0.ft
println("Three feet is $threeFeet meters")
// prints "Three feet is 0.914399970739201 meters"

siddharth makadiya

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google

0 comments:

Popular Posts