| Start |
|---|
| Serializable and serialVersionUid in Kotlin To create the The most concise bytecode comes from a You can also use these forms, each with a side effect of having getter/setter methods which are not necessary for serialization... This creates the static field but also creates a getter as well This creates the static field but also creates a static getter as well But all work as a method of adding the |
| Delegate to a class without providing it in the public constructor Assume you want to delegate to a class (opens new window) 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 constructor parameters. However, there is a way to do it, as given in this answer (opens new window): With this, you can just call the constructor of This example is based on this SO question (opens new window). |
| Use let or also to simplify working with nullable objects
This will print The difference between Now why this is useful, you ask? Because if you call a method which can return This piece of code will only run the |
Filtering a list |
| Fluent methods in Kotlin Fluent methods in Kotlin can be the same as Java: But you can also make them more functional by creating an extension function such as: Which then allows more obviously fluent functions: |
| Use apply to initialize objects or to achieve method chaining The documentation of calls the specified function block with While the kdoc is not so helpful This is the same as writing this: |
| Creating DTOs (POJOs/POCOs) Data classes in kotlin are classes created to do nothing but hold data. Such classes are marked as The code above creates a
Just as with a function, default values can also be specified: More details can be found here Data Classes (opens new window). |
Read Article
https://develop.horje.com/learn/1434/reference