Image for post
Image for post
Illustration by Virginia Poltrack

#31DaysOfKotlin — Week 1 Recap

Florina Muntenescu
Apr 3, 2018 · 4 min read

Day 1: Elvis operator

Handling nulls in style? Check out the elvis operator ?:, to cut your “null-erplate”. It’s just a small bit of syntax sugar to replace nulls with a default value or even return! Docs: Elvis operator.

val name: String = person.name ?: “unknown”
val age = person.age ?: return

Day 2: String templates

Formatting Strings? Refer to variables and expressions in string literals by putting $ in front of the variable name. Evaluate expressions using ${expression}. Docs: string templates.

val language = “Kotlin”// “Kotlin has 6 characters”
val text = “$language has ${language.length} characters”

Day 3: Destructuring declarations

Now with prisms? Android KTX uses destructuring to assign the component values of a color. You can use destructuring in your classes, or extend existing classes to add destructuring. Docs: destructuring declarations.

// now with prisms
val (red, green, blue) = color
// destructuring for squares
val (left, top, right, bottom) = rect
// or more pointedly
val (x, y) = point

Day 4: When expressions

A switch statement with superpowers? Kotlin’s when expression can match on just about anything. Literal values, enums, ranges of numbers. You can even call arbitrary functions! Docs: when

class Train(val cargo: Number?) {
override fun toString(): String {
return when (cargo) {
null, 0 -> "empty"
1 -> "tiny"
in 2..10 -> "small"
is Int -> "big inty"
else -> "$cargo"
}
}
}

Day 5: For loops, range expressions and destructuring

For loops get superpowers when used with two other Kotlin features: range expressions and destructuring. Docs: ranges, destructuring.

// iterating in the range 1 to 100
for(i in 1..100) {…}
// iterating backwards, in the range 100 to 1
for(i in 100 downTo 1){…}
// iterating over an array, getting every other element
val array = arrayOf(“a”, “b”, “x”)
for(i in 1 until array.size step 2 ){…}
// iterating over an array with the item index and destructuring
for((index, element) in array.withIndex()) {…}
// iterating over a map
val map = mapOf(1 to “one”, 2 to “two”)
for( (key, value) in map){…}

Day 6: Properties

In Kotlin, classes can have mutable and read-only properties, with getters and setters generated by default. You can also implement custom ones if required. Docs: properties.

class User {
// properties
val id: String = “” // immutable. just getter
var name: String = “” // default getter and setter var surname: String = “” // custom getter, default setter
get() = surname.toUpperCase() // custom getter declaration
var email: String = “” // default getter, custom setter
set(value) { // custom setter declaration
// “value” = name of the setter parameter
// “field” = property’s backing field; generated
if(isEmailValid(value)) field = value
}
}

Day 7: Data classes and equality

Creating classes with one role: to hold data? Mark them as “data” classes. The default implementation of equals() is generated (so are hashCode(), toString(), and copy()) and checks for structural equality. Docs: data classes, equality

data class User(
val name: String,
val email: String,
val address: Address,

)
public class UserListDiffCallback: DiffUtil.Callback() { override fun areContentsTheSame(
oldItemPosition: Int,
newItemPosition: Int
): Boolean {
// use the generated equals method
return newUserList[newItemPosition] ==
oldUserList[oldItemPosition])
}

Android Developers

The official Android Developers publication on Medium

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app