Kotlin 2025

From bibbleWiki
Revision as of 03:27, 10 March 2025 by Iwiseman (talk | contribs) (Now The Notes)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Introduction

It has been a while since I last looked at Kotlin. I think I am still and rust fan but always loved Kotlin on the phone. It is probably 4 years and one stroke since I looked at this and my little grey cells need a refresh

Compose

I have covered this a bit in Android Compose Example Surely a major improvement

MVI

I have used MvvM in the past but MVI now looks to be a better choice for me at the moment. You would have to have done a lot of projects to know for sure. I only dip my toe it but see Andoroid MVI Example

Now The Notes

Functions

This is just a refresher of the functions from Kotlin. We have

fun timesTwo(it: Int) = it * 2 // Normal fun
val timeThree = { it: Int -> it * 3} // Lambda
val timesFour = (fun (it: Int) = it * 4) // Anonymous function

To pass a function we can use function references. We do this by using the double full colon

fun main() {
    val result3 = perform(::timesTwo)
}

fun perform(operation: (Int) -> Int): Int {
  return operation(5)
}

Destructuring

We can destructure data classes in kotlin. e.g.

data class User(val id: Int, val name: String)
val user = User(1234,"Jacob")
val (userId, userName) = user

But we can do this for objects too by adding components e.g.

operator fun LocalDate.component1() = year
operator fun LocalDate.component2() = monthValue
operator fun LocalDate.component3() = dayOfMonth

val today = LocalDate.parse("2016-02-15)
val (year, month, day) = today