150: Learning Kotlin – Returns, Jumps & Labels

Shownotes

Download

Code

data class Customer(val isPlatinum: Boolean)

fun main() {

    val customer = Customer(false)

    println("Number of points customer has: ${calculatePoints(customer)}")

    // Break out of the loop once we're over 25
    for (i in 1..100) {
        if (i > 25) {
            break
        } else {
            println(i)
        }
    }


    // Skip all even numbers
    for (i in 1..100) {
        if (i % 2 == 0) {
            continue
        } else {
            println(i)
        }
    }


    // Break out of the outer loop (which breaks out of the inner too) using a label
    donn@ for (i in 1..100) {
        for (j in 100..200) {
            if (j > 150) break@donn // This will break out of the inner loop and outer loop
            else println("i: $i, j: $j")
        }
    }

    // Continue processing the next outer loop value when a condition is met.
    donn@ for (i in 1..100) {
        for (j in 100..200) {
            if (j > 150) continue@donn // This will break out of the inner loop and outer loop
            else println("i: $i, j: $j")
        }
    }

    // returns with label
    example1()
    example2()

    println("I'm done processing!")
}

fun calculatePoints(customer: Customer): Int {
    if (customer.isPlatinum) {
        return 100000
    } else {
        return 10
    }
}


fun example1() {
    listOf(1, 2, 3, 4, 5, 6, 7, 8, 9).forEach {
        if (it > 7) {
            return
        } else {
            println(it)
        }
    }
    println("This wont print :( because return exited the bar() function")
}

fun example2() {
    listOf(1, 2, 3, 4, 5, 6, 7, 8, 9).forEach bin@ {
        if (it > 7) {
            return@bin
        } else {
            println(it)
        }
    }
    println("This will print! :) return exited the forEach!")
}

Sponsors 🙏

  • Nevercode

    • Nevercode is taking Flutter revolution extremely serious and is prepared to offer kick-ass CI/CD for Flutter projects with codemagic.io. Check it out and get started at https://codemagic.io/
  • Sentry.io

    • Sentry tells you about errors in your code before your customers have a chance to encounter them. Check them out at: https://sentry.io/for/android/

Contact

149: Learning Kotlin: inline classes and Type driven design

In this quick fragment, Kaushik talks about the new Kotlin 1.3 experimental feature “inline classes” and how it helps with Type driven design.

Download

Shownotes

Type driven design resources

Kotlin KEEP

vs typealias

Sponsors 🙏

Contact

138: Decompress – kotlin scripting

In this mini fragment, Donn asks KG about a recent hackathon side project that he worked on with Kotlin scripting.

We go into kotlin scripting (using kscript), pros cons, setting it up and more. We’ve added links some interesting resources, so you want to make sure you check out the shownotes for this one, so you too can explore kotlin scripting and see if it’s a fit for you.

Download

Shownotes

Existing solutions for poll/vote tabulation

Misc


// slick kotlin usage override fun onCreate(savedInstanceState: Bundle?) = super.onCreate(savedInstanceState).also { setContentView(R.layout.activity_something) // ... }

Sponsors

Contact

136: Kotlin Extension Functions

In this fragment episode, Donn talks about Kotlin extension functions. He discusses what they are, how to build them, why they’re useful, how to organize them, visibility and how to call them Java and much more.

Download

Show Notes

`

import android.view.View

fun View.gone() {
    this.visibility = View.GONE
}

fun View.visible() {
    this.visibility = View.VISIBLE
}

`

Contact

122: Learning Kotlin – typealias

In this episode of learning kotlin, we look at the typealias keyword. The typealias keyword allows you to provide alternate names for existing types and and function types. Learn how, why and when you can use it in this episode

Direct Download

Show Notes

Sponsors

  • Microsoft AppCenter – Sign up now on appcenter.ms and spend less time managing your app lifecycle and more time coding.

Contact