Created
July 30, 2019 09:39
-
-
Save ch8n/d8277229bffdd4ddff2facf8bba4f258 to your computer and use it in GitHub Desktop.
Under the hood, Kotlin is actually calling the component function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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