Share
Embed
The Lesser-Known Kotlin Features
The lesser-known Kotlin features
Kotlin is a modern programming language initially created as a "better Java." However, Kotlin provides not just a better syntax but a ton of new interesting features. In this session, you will learn about the subset of the lesser know features that might look obscure at first sight. How does the reified keyword in Kotlin work? What is the difference between inline and crossinline, and why is noinline required? Why do we need to indicate some types as "definitely non-nullable"? What's the deal with context receivers, and what's the lambda with the receiver? After this session, you will have an idea about the hidden Kotlin gems features and when to apply these advanced features.
Anton Arhipov
September 13, 2022
Transcript
104 slides
-
class Figure( val width: Int, val height: Int, val depth:
Int, val color: Color, val description: String, ) -
class Figure( val width: Int, val height: Int, val depth:
Int, val color: Color, val description: String, ) Figure(1, 2, 3, Color.RED, "Red brick") Figure(1, 1, 1, Color.GREEN, "Green cube") -
class Figure( val width: Int, val height: Int, val depth:
Int, val color: Color, val description: String, ) Figure(1, 2, 3, Color.RED, "Red brick") Figure(1, 1, 1, Color.GREEN, "Green cube") Figure(1, 1, 1, Color.BLUE, "Blue cube") -
class Figure( val width: Int, val height: Int, val depth:
Int, val color: Color, val description: String, ) Figure(1, 2, 3, Color.RED, "Red brick") Figure(1, 1, 1, Color.GREEN, "Green cube") Figure(1, 1, 1, Color.BLUE, "Blue cube") Figure(1, 1, 1, Color.BLACK, "Black cube") -
class Figure( val width: Int, val height: Int, val depth:
Int, val color: Color, val description: String, ) Figure(1, 2, 3, Color.RED, "Red brick") Figure(1, 1, 1, Color.GREEN, "Green cube") Figure(1, 1, 1, Color.BLUE, "Blue cube") Figure(1, 1, 1, Color.BLACK, "Black cube") -
class Figure( val width: Int = 1, val height: Int
= 1, val depth: Int = 1, val color: Color = Color.BLACK, val description: String = "This is ${color} figure", ) Figure(1, 2, 3, Color.RED, "Red brick") Figure(1, 1, 1, Color.GREEN, "Green cube") Figure(1, 1, 1, Color.BLUE, "Blue cube") Figure(1, 1, 1, Color.BLACK, "Black cube") -
class Figure( val width: Int = 1, val height: Int
= 1, val depth: Int = 1, val color: Color = Color.BLACK, val description: String = "This is ${color} figure", ) Figure(1, 2, 3, Color.RED, "Red brick") Figure(Color.GREEN) Figure(Color.BLUE) Figure(Color.BLACK) -
class Figure( val width: Int = 1, val height: Int
= 1, val depth: Int = 1, val color: Color = Color.BLACK, val description: String = "This is ${color} figure", ) Figure(1, 2, 3, Color.RED, "Red brick") Figure(Color.GREEN) Figure(Color.BLUE) Figure(Color.BLACK) Would not compile! -
class Figure( val width: Int = 1, val height: Int
= 1, val depth: Int = 1, val color: Color = Color.BLACK, val description: String = "This is ${color} figure", ) Figure(1, 2, 3, Color.RED, "Red brick") Figure(color = Color.GREEN) Figure(color = Color.BLUE) Figure(color = Color.BLACK) -
class Figure( val width: Int = 1, val height: Int
= 1, val depth: Int = 1, val color: Color = Color.BLACK, val description: String = "This is ${color} figure", ) Figure(width = 1, height = 2, depth = 3, Color.RED, "Red brick") Figure(color = Color.GREEN) Figure(color = Color.BLUE) Figure(color = Color.BLACK) -
class Figure( val width: Int = 1, val height: Int
= 1, val depth: Int = 1, val color: Color = Color.BLACK, val description: String = "This is ${color} figure", ) Figure(width = 1, height = 2, depth = 3, Color.RED, "Red brick") Figure(color = Color.GREEN) Figure(color = Color.BLUE) Figure(color = Color.BLACK) -
foo { bar { baz = "Hello!" qux = quux
{ corge = "Blah" } } } -
foo { bar { baz = "Hello!" qux = quux
{ corge = "Blah" } } } -
foo { bar { baz = "Hello!" qux = quux
{ corge = "Blah" } } } -
foo { bar { baz = "Hello!" qux = quux
{ corge = "Blah" } } } -
foo { bar(grault = 1) { baz = "Hello!" qux
= quux { corge = "Blah" } } } -
foo { bar(grault = 1) { baz = "Hello!" qux
= quux { corge = Blah() } } } -
foo { bar(grault = 1) { baz = "Hello!" qux
= quux { corge = Blah() } } } -
foo { bar(grault = 1) { baz = "Hello!" qux
= quux { corge = Blah() } } } Named arguments -
foo { bar(grault = 1) { baz = "Hello!" qux
= quux { corge = Blah() } } } Higher-order functions -
foo { bar(grault = 1) { baz = "Hello!" qux
= quux { corge = Blah() } } } Trailing lambda -
foo { bar(grault = 1) { baz = "Hello!" qux
= quux { corge = Blah() } } } Extension function it: Foo fun Foo.bar(c: () -> Unit){ ... } it. -
foo { bar(grault = 1) { baz = "Hello!" qux
= quux { corge = Blah() } } } SAM-conversion fun quux(r: Runnable){ ... } -
foo { bar(grault = 1) { baz = "Hello!" qux
= quux { corge = Blah() } } } Lambda with receiver this: Foo fun foo(c: Foo.() -> Unit){ ... } -
foo { bar(grault = 1) { baz = "Hello!" qux
= quux { corge = Blah() } } } Lambda with receiver this: Foo fun foo(c: Foo.() -> Unit){ ... } -
foo { bar(grault = 1) { baz = "Hello!" qux
= quux { corge = Blah() } } } Lambda with receiver this: Foo fun foo(c: Foo.() -> Unit){ ... } DEMO TIME? -
Kotlin DSL in TeamCity project { vcsRoot(ApplicationVcs) buildType { id("Application")
name = "Application" vcs { root(ApplicationVcs) } artifactRules = "target/*jar" steps { maven { goals = "clean package" } } triggers { vcs {} } dependencies { snapshot(Library) {} -
@SpringBootApplication class DemoApplication fun main(args: Array<String>) { runApplication<DemoApplication>(*args) } inline
fun <reified T : Any> runApplication(vararg args: String): ConfigurableApplicationContext = SpringApplication.run(T :: class.java, *args) -
@SpringBootApplication class DemoApplication fun main(args: Array<String>) { runApplication<DemoApplication>(*args) } inline
fun <reified T : Any> runApplication(vararg args: String): ConfigurableApplicationContext = SpringApplication.run(T :: class.java, *args) inline fun <reified T : Any> runApplication(vararg args: String, init: SpringApplication.() -> Unit): ConfigurableApplicationContext = SpringApplication.run(T :: class.java).apply(init).run(*args) -
@SpringBootApplication class DemoApplication fun main(args: Array<String>) { runApplication<DemoApplication>(*args) } inline
fun <reified T : Any> runApplication(vararg args: String): ConfigurableApplicationContext = SpringApplication.run(T :: class.java, *args) inline fun <reified T : Any> runApplication(vararg args: String, init: SpringApplication.() -> Unit): ConfigurableApplicationContext = SpringApplication.run(T :: class.java).apply(init).run(*args) -
@SpringBootApplication class DemoApplication fun main(args: Array<String>) { runApplication<DemoApplication>(*args) { addInitializers(beans)
} } val beans = beans { bean { CommandLineRunner { println("start data initialization ... ") val repository = ref<MyRepository>() repository.save(Message(text = "this is the first message!")) repository.save(Message(text = "this is the second message!")) } } } this: SpringApplication -
@SpringBootApplication class DemoApplication fun main(args: Array<String>) { runApplication<DemoApplication>(*args) { addInitializers(beans)
} } val beans = beans { bean { CommandLineRunner { println("start data initialization ... ") val repository = ref<MyRepository>() repository.save(Message(text = "this is the first message!")) repository.save(Message(text = "this is the second message!")) } } } this: SpringApplication this: BeanDefinitionDsl this: BeanDefinitionDsl.BeanSupplierContext it: Array<String> -
@SpringBootApplication class DemoApplication fun main(args: Array<String>) { runApplication<DemoApplication>(*args) { addInitializers(beans)
} } val beans = beans { bean { CommandLineRunner { println("start data initialization ... ") val repository = ref<MyRepository>() repository.save(Message(text = "this is the first message!")) repository.save(Message(text = "this is the second message!")) } } } -
val list = buildList<String> { } @SinceKotlin("1.6") @WasExperimental(ExperimentalStdlibApi :: class)
@kotlin.internal.InlineOnly @Suppress("DEPRECATION") public inline fun <E> buildList(@BuilderInference builderAction: MutableList<E>.() - > Unit): List<E> contract { callsInPlace(builderAction, InvocationKind.EXACTLY_ONCE) } return buildListInternal(builderAction) } -
val list = buildList { val x = get(0) }
Not enough type information -
val list = buildList { val x: Int = get(0)
} this: MutableList<Int> -
val list = buildList { add("hello!") val x = get(0)
} this: MutableList<String> -
val list = buildList { add("hello!") val x: Int =
get(0) } this: MutableList<String> -
val list = buildList { add("hello!") val x: Int =
get(0) } this: MutableList<String> Type mismatch -
val list = buildList { val x = get(0) doSomething(x)
} this: MutableList<Int> fun doSomething(x: Int){…} -
fun main() { val ints = listOf(1,2,3,4) ints.forEveryOther { println(it)
} } inline fun Collection<Int>.forEveryOther(block: (Int) -> Unit) { for ((i, element) in withIndex()) { if (i % 2 == 0) { block(element) } } } -
fun main() { val ints = listOf(1,2,3,4) ints.forEveryOther { println(it)
} } inline fun Collection<Int>.forEveryOther(block: (Int) -> Unit) { for ((i, element) in withIndex()) { if (i % 2 == 0) { block(element) } } } Inlines everything! -
fun main() { val ints = listOf(1,2,3,4) for ((i, element)
in withIndex()) { if (i % 2 == 0) { println(element) } } } inline fun Collection<Int>.forEveryOther(block: (Int) -> Unit) { for ((i, element) in withIndex()) { if (i % 2 == 0) { block(element) } } } -
fun main() { val ints = listOf(1,2,3,4) ints.forEveryOther { println(it)
} } inline fun Collection<Int>.forEveryOther(block: (Int) -> Unit) { for ((i, element) in withIndex()) { val task = Runnable { if (i % 2 == 0) { block(element) } } task.run() } } -
fun main() { val ints = listOf(1,2,3,4) ints.forEveryOther { println(it)
} } inline fun Collection<Int>.forEveryOther(block: (Int) -> Unit) { for ((i, element) in withIndex()) { val task = Runnable { if (i % 2 == 0) { block(element) } } task.run() } } -
fun main() { val ints = listOf(1,2,3,4) ints.forEveryOther { println(it)
} } inline fun Collection<Int>.forEveryOther(block: (Int) -> Unit) { for ((i, element) in withIndex()) { val task = Runnable { if (i % 2 == 0) { block(element) } } task.run() } } -
fun main() { val ints = listOf(1,2,3,4) ints.forEveryOther { println(it)
} } inline fun Collection<Int>.forEveryOther(block: (Int) -> Unit) { for ((i, element) in withIndex()) { val task = Runnable { if (i % 2 == 0) { block(element) } } task.run() } } -
fun main() { val ints = listOf(1,2,3,4) ints.forEveryOther { if(it
== 3) return println(it) } } inline fun Collection<Int>.forEveryOther(block: (Int) -> Unit) { for ((i, element) in withIndex()) { val task = Runnable { if (i % 2 == 0) { block(element) } } task.run() } Non-local return Returns from main -
fun main() { val ints = listOf(1,2,3,4) ints.forEveryOther l@{ if(it
== 3) return@l println(it) } } inline fun Collection<Int>.forEveryOther(block: (Int) -> Unit) { for ((i, element) in withIndex()) { val task = Runnable { if (i % 2 == 0) { block(element) } } task.run() } Local return Returns from lambda -
fun main() { val ints = listOf(1,2,3,4) ints.forEveryOther l@{ if(it
== 3) return@l println(it) } } inline fun Collection<Int>.forEveryOther(block: (Int) -> Unit) { for ((i, element) in withIndex()) { val task = Runnable { if (i % 2 == 0) { block(element) } } task.run() } -
fun main() { val ints = listOf(1,2,3,4) ints.forEveryOther l@{ if(it
== 3) return@l println(it) } } inline fun Collection<Int>.forEveryOther(crossinline block: (Int) -> Unit) { for ((i, element) in withIndex()) { val task = Runnable { if (i % 2 == 0) { block(element) } } task.run() } -
fun main() { val ints = listOf(1,2,3,4) ints.forEveryOther { if(it
== 3) return println(it) } } inline fun Collection<Int>.forEveryOther(crossinline block: (Int) -> Unit) { for ((i, element) in withIndex()) { val task = Runnable { if (i % 2 == 0) { block(element) } } task.run() } -
fun main() { val ints = listOf(1,2,3,4) ints.forEveryOther { if(it
== 3) return println(it) } } inline fun Collection<Int>.forEveryOther(crossinline block: (Int) -> Unit) { for ((i, element) in withIndex()) { val task = Runnable { if (i % 2 == 0) { block(element) } } task.run() } -
fun main() { val ints = listOf(1,2,3,4) ints.forEveryOther l@{ if(it
== 3) return@l println(it) } } inline fun Collection<Int>.forEveryOther(crossinline block: (Int) -> Unit) { for ((i, element) in withIndex()) { val task = Runnable { if (i % 2 == 0) { block(element) } } task.run() } -
fun main() { val ints = listOf(1,2,3,4) ints.forEveryOther l@{ if(it
== 3) return@l println(it) } } inline fun Collection<Int>.forEveryOther(crossinline block: (Int) -> Unit) { for ((i, element) in withIndex()) { val task = Runnable { if (i % 2 == 0) { block(element) } } task.run() } Inlines the loop Lambda is compiled to a separate class -
fun main() { val ints = listOf(1,2,3,4) for ((i, element)
in withIndex()) { val task: Runnable = (Runnable)Kt$inlined$forEveryOther(i, element) task.run() } } class Kt$inlined$forEveryOther implements Runnable { . .. public final run(){ if (i % 2 == 0) { if(element ! = 3) System.out.println(element) } } } -
inline fun Collection<Int>.forEveryOther(blockA: (Int) -> Unit, blockB: (Int) -> Unit,
) { ... doSomething(blockB) } -
inline fun Collection<Int>.forEveryOther(blockA: (Int) -> Unit, blockB: (Int) -> Unit,
) { ... doSomething(blockB) } fun doSomething(block: (Int) -> Unit) { ... } -
inline fun Collection<Int>.forEveryOther(blockA: (Int) -> Unit, blockB: (Int) -> Unit,
) { ... doSomething(blockB) } fun doSomething(block: (Int) -> Unit) { ... } -
inline fun Collection<Int>.forEveryOther(blockA: (Int) -> Unit, noinline blockB: (Int) ->
Unit, ) { ... doSomething(blockB) } fun doSomething(block: (Int) -> Unit) { ... } -
inline fun Collection<Int>.forEveryOther(blockA: (Int) -> Unit, noinline blockB: (Int) ->
Unit, ) { ... doSomething(blockB) } fun doSomething(block: (Int) -> Unit) { ... } -
@SpringBootApplication class DemoApplication fun main(args: Array<String>) { runApplication<DemoApplication>(*args) } inline
fun <reified T : Any> runApplication(vararg args: String): ConfigurableApplicationContext = SpringApplication.run(T :: class.java, *args) -
@SpringBootApplication class DemoApplication fun main(args: Array<String>) { runApplication<DemoApplication>(*args) } inline
fun <reified T : Any> runApplication(vararg args: String): ConfigurableApplicationContext = SpringApplication.run(T :: class.java, *args) -
inline fun <reified T> printType() { println(T : : class.java)
} fun printStringType(){ printType<String>() } -
inline fun <reified T> printType() { println(T :: class.java) }
fun printStringType(){ printType<String>() } public static final void printType() { int $i$f$printType = 0; Intrinsics.reifiedOperationMarker(4, "T"); Class var1 = Object.class; System.out.println(var1); } public static final void printStringType() { int $i$f$printType = false; Class var1 = String.class; System.out.println(var1); } -
inline fun <reified T> printType() { println(T :: class.java) }
fun printStringType(){ printType<String>() } public static final void printType() { int $i$f$printType = 0; Intrinsics.reifiedOperationMarker(4, "T"); Class var1 = Object.class; System.out.println(var1); } public static final void printStringType() { int $i$f$printType = false; Class var1 = String.class; System.out.println(var1); } -
inline fun <reified T> printType() { println(T :: class.java) }
fun printStringType(){ printType<String>() } public static final void printType() { int $i$f$printType = 0; Intrinsics.reifiedOperationMarker(4, "T"); Class var1 = Object.class; System.out.println(var1); } public static final void printStringType() { int $i$f$printType = false; Class var1 = String.class; System.out.println(var1); } -
inline fun <reified T> calculate(value: Float): T { return when
(T :: class) { Int :: class -> value.toInt() as T Float :: class -> value as T else -> throw IllegalArgumentException("$value is neither Float or Int") } } val intValue: Int = calculate(123f) val floatValue: Float = calculate(123f) -
val list: List<Any> = listOf(1, "Hello", Color.RED) val strings: List<String>
= list.filterIsInstance<String>() -
val list: List<Any> = listOf(1, "Hello", Color.RED) val strings: List<String>
= list.filterIsInstance<String>() public inline fun <reified R> Iterable < *> .filterIsInstance(): List<R> { return filterIsInstanceTo(ArrayList<R>()) } -
Type-safe builders Functional literal (aka lambda) with receiver Builder inference
Inline, crossinline, noinline (Rei fi ed) generics Some stdlib examples -
Type-safe builders Functional literal (aka lambda) with receiver Builder inference
Inline, crossinline, noinline (Rei fi ed) generics Some stdlib examples -
Kotlin { YouTube = "youtube.com/kotlin" Slack = "slack.kotl.in" } me
{ name = "Anton Arhipov" twitter = "@antonarhipov" slides = speakerdeck.com/antonarhipov } this: Person this: Project