Daily used Kotlin Code Snippet

In past month i did some work on Kotlin and some article you can see on this trending topic. So today i am here with the list of daily used Kotlin code snippet which every developer need to know.

Daily used Kotlin Code Snippet

It is a collection of random and frequently used idioms in Kotlin. If you have a favorite idiom, contribute it by sending a comment below.

List of Daily used Kotlin Code Snippet

Creating DTOs (POJOs/POCOs)

  1. data class Customer(val name: String, val email: String)
provides a Customer class with the following functionality:

  • getters (and setters in case of var{: .keyword }s) for all properties
  • equals()
  • hashCode()
  • toString()
  • copy()
  • component1()component2(), …, for all properties (see Data classes)

Default values for function parameters

  1. fun foo(a: Int = 0, b: String = "") { ... }
Do you know : What is Kotlin Native?

Filtering a list

  1. val positives = list.filter { x -> x > 0 }
Or alternatively, even shorter:

  1. val positives = list.filter { it > 0 }
Do you use this : Koin – Kotlin Dependency Injection

String Interpolation

  1. println("Name $name")
 
Source: Top 20 Daily used Kotlin Code Snippet

Comments