to kotlin - use the nullable type // Manually improved to use the nullable type fun hasQuality(stream: Stream?): Boolean { var quality = false val query: String? = stream?.uri?.query if (query != null) { quality = query.contains(“quality”) } return quality }
to kotlin - use the nullable type - if as expression // If as an expression fun hasQuality(stream: Stream?): Boolean { val query: String? = stream?.uri?.query return if(query != null) { query.contains(“quality”) } else false }
to kotlin - use the nullable type - if as expression - no explicit null-checks // No explicit null-checks fun hasQuality(stream: Stream?): Boolean { return stream?.uri?.query?.contains(“quality”) ?: false }
to kotlin - use the nullable type - if as expression - no explicit null-checks - three styles of the function // 1. Regular function fun hasQuality(stream: Stream?): Boolean { return stream?.uri?.query?.contains(“quality”) ?: false } // 2. The return type us inferred from the expression fun hasQuality(stream: Stream?) = stream?.uri?.query?.contains(“quality”) == true // 3 Extension function fun Stream.hasQuality() = uri?.query?.contains(“quality”) == true myStream?.hasQuality()
from) { if (from instanceof Integer) { if ((int) from <= 5 && (int) from > 0) { return new Grade((int) from); } } else if (from instanceof Character) { if ((char) from <= 'f' && (char) from >= 'a') { return new Grade((char) from); } } else if (from == "G" || from == "VG" || from == "MVG"){ return new Grade((String) from); } return Grade.FAIL; }
parse(from: Any): Grade { if (from is Int) { if (from as Int <= 5 && from as Int > 0) { return Grade(from as Int) } } else if (from is Char) { if (from as Char <= 'f' && from as Char >= 'a') { return Grade(from as Char) } } else if (from == "G" || from == "VG" || from == "MVG"){ return Grade(from as String) } return Grade.FAIL }
smart cast fun parse(from: Any): Grade { if (from is Int) { if (from <= 5 && from > 0) { return Grade(from) } } else if (from is Char) { if (from <= 'f' && from >= 'a') { return Grade(from) } } else if (from == "G" || from == "VG" || from == "MVG"){ return Grade(from as String) } return Grade.FAIL }
smart cast - ranges fun parse(from: Any): Grade { if (from is Int) { if (from in 1..5) { return Grade(from) } } else if (from is Char) { if (from in 'a'..’f’) { return Grade(from) } } else if (from == "G" || from == "VG" || from == "MVG"){ return Grade(from as String) } return Grade.FAIL }
smart cast - ranges - when fun parse(from: Any): Grade { return when (from) { is Int -> if (from in 1..5) Grade(from) else Grade.FAIL is Char -> if (from in ‘a'..’f’) Grade(from) else Grade.FAIL “G”, “VG”, “MVG" -> Grade(from as String) else -> Grade.FAIL }
smart cast - ranges - when - done fun parse(from: Any) = when (from) { in 1..5 -> Grade(from as Int) in ‘a’..’f’ -> Grade(from as Char) “G”, “VG”, “MVG" -> Grade(from as String) else -> Grade.FAIL }
- copy - deconstructing data class Person(val name: String, val age: Int) val bob = Person("Bob", 33) val workingBob = bob.copy() val agingBob = bob.copy(age=66) val (_,age) = bob
- copy - deconstructing - operation overloading data class Person(val name: String, val age: Int) operator fun Person.plus(extra:String) = copy(“$name $extra”) val bob = Person("Bob", 33) val fullNameBob = bob + “Dahlberg”