Skip to content

Instantly share code, notes, and snippets.

@ch8n
Created July 30, 2019 09:39
Show Gist options
  • Select an option

  • Save ch8n/d8277229bffdd4ddff2facf8bba4f258 to your computer and use it in GitHub Desktop.

Select an option

Save ch8n/d8277229bffdd4ddff2facf8bba4f258 to your computer and use it in GitHub Desktop.
Under the hood, Kotlin is actually calling the component function
//example
data class Items(val one: String, val two: String)
val items= Items("1","2")
val (one, two) = items
//converts to
val one = items.component1
val one = items.component2
// means a getter function/operator/field is already present in those
// class which automatically popluates these fields
// What the creators of Kotlin did was actually implement
// 5 extension functions on many of the classes in the
// Collections.kt library that we commonly use.
operator fun <T> List<T>.component1(): T
operator fun <T> List<T>.component2(): T
operator fun <T> List<T>.component3(): T
operator fun <T> List<T>.component4(): T
operator fun <T> List<T>.component5(): T
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment