Tutorial by Examples

Data classes in kotlin are classes created to do nothing but hold data. Such classes are marked as data: data class User(var firstname: String, var lastname: String, var age: Int) The code above creates a User class with the following automatically generated: Getters and Setters for all prope...
val list = listOf(1,2,3,4,5,6) //filter out even numbers val even = list.filter { it % 2 == 0 } println(even) //returns [2,4]
Assume you want to delegate to a class but you do not want to provide the delegated-to class in the constructor parameter. Instead, you want to construct it privately, making the constructor caller unaware of it. At first this might seem impossible because class delegation allows to delegate only to...
To create the serialVersionUID for a class in Kotlin you have a few options all involving adding a member to the companion object of the class. The most concise bytecode comes from a private const val which will become a private static variable on the containing class, in this case MySpecialCase: ...
Fluent methods in Kotlin can be the same as Java: fun doSomething() { someOtherAction() return this } But you can also make them more functional by creating an extension function such as: fun <T: Any> T.fluently(func: ()->Unit): T { func() return this } Which the...
let in Kotlin creates a local binding from the object it was called upon. Example: val str = "foo" str.let { println(it) // it } This will print "foo" and will return Unit. The difference between let and also is that you can return any value from a let block. also ...
The documentation of apply says the following: calls the specified function block with this value as its receiver and returns this value. While the kdoc is not so helpful apply is indeed an useful function. In layman's terms apply establishes a scope in which this is bound to the object you ca...

Page 1 of 1