Learn Kotlin from Swift perspective

Muhammad Alfiansyah
3 min readJul 27, 2024

--

Photo by Himesh Kumar Behera on Unsplash

I believe if we learn with comparison, we will learn faster so here I am, learning Kotlin from Swift perpective.

What is and what is not in the Swift ?

Why there is and why there is not in Swift ?

How I use them in Swift or Kotlin ?

If there is no equality can I build it my self ?

So this is the Comparison Table I have made, to make Kotlin more fun to learn.

Overloading

fun add(a: Int, b: Int): Int {
return a + b
}

fun add(a: Double, b: Double): Double {
return a + b
}
func add(a: Int, b: Int) -> Int {
return a + b
}

func add(a: Double, b: Double) -> Double {
return a + b
}s

Abstract

abstract class Animal(var name: String, var weight: Double, var age: Int, var isCarnivore: Boolean){

fun eat(){
println("$name sedang makan !")
}

fun sleep(){
println("$name sedang tidur !")
}
}
protocol Animal {
var name: String { get set }
var weight: Double { get set }
var age: Int { get set }
var isCarnivore: Bool { get set }

func eat()
func sleep()
}

extension Animal {
func eat() {
print("\(name) sedang makan !")
}

func sleep() {
print("\(name) sedang tidur !")
}
}

Extension Function

fun Int.square(): Int {
return this * this
}
extension Int {
func square() -> Int {
return self * self
}
}

Extension Variable

val String.length: Int
get() = this.length
extension String {
var length: Int {
return self.count
}
}

Visibility Modifier

public class PublicClass {}
internal class InternalClass {}
protected class ProtectedClass {}
private class PrivateClass {}
public class PublicClass {}
internal class InternalClass {}
fileprivate class FilePrivateClass {}
private class PrivateClass {}

Late init

lateinit var text: String
var text: String?

Lazy Initialization

val text: String by lazy {
"Hello"
}
lazy var text: String = "Hello"

Primary Constructor

class Person(val name: String)
struct Person {
var name: String
}

// OR

struct Person {
var name: String
init(name: String) {
self.name = name
}
}

Secondary Constructor

class Person {
var name: String

constructor(name: String) {
this.name = name
}

constructor() {
this.name = "John Doe"
}
}
class Person {
var name: String
init(name: String) {
self.name = name
}
convenience init() {
self.init(name: "John Doe")
}
}

Default Constructor

class Person(val name: String = "John Doe")
struct Person {
var name: String = "John Doe"
}

// OR

struct Person {
var name: String
init(name: String = "John Doe") {
self.name = name
}
}

Property Delegation

import kotlin.reflect.KProperty

class DelegateName {
private var value: String = "Default"

operator fun getValue(classRef: Any?, property: KProperty<*>) : String {
println("Fungsi ini sama seperti getter untuk properti ${property.name} pada class $classRef")
return value
}

operator fun setValue(classRef: Any?, property: KProperty<*>, newValue: String){
println("Fungsi ini sama seperti setter untuk properti ${property.name} pada class $classRef")
println("Nilai ${property.name} dari: $value akan berubah menjadi $newValue")
value = newValue
}
}

class Animal {
var name: String by DelegateName()
}
@propertyWrapper
struct DelegateName {
private var value: String

var wrappedValue: String {
get {
print("Fungsi ini sama seperti getter untuk properti \(propertyName)")
return value
}
set {
print("Fungsi ini sama seperti setter untuk properti \(propertyName)")
print("Nilai \(propertyName) dari: \(value) akan berubah menjadi \(newValue)")
value = newValue
}
}

private var propertyName: String = "Default"

init(wrappedValue: String) {
self.value = wrappedValue
}

init() {
self.value = "Default"
}
}

class Animal {
@DelegateName var name: String
}

Next is Special Class and Collection

--

--