https://kotlinlang.org logo
Title
r

Rick Pendrick

09/30/2019, 3:00 PM
I am curious, in this code:
fun String.asIndex(): Int = withIndex().sumBy { ... }
Why does the call to withIndex() not need use "it" or "this" ? <<I am newbie, obviously!>>
m

marstran

09/30/2019, 3:09 PM
Because
this
is implicit. You are in the context of a String inside the body of an extension function on String.
So it's equivalent to
fun String.asIndex(): Int = this.withIndex().sumBy { ... }
, but you just don't have to write
this
.
r

Rick Pendrick

09/30/2019, 3:12 PM
Thank you. It took me a bit to figure it was implied, as a new person I was just reading open source code.