I tried `var current: Body? = this@Body` but the c...
# getting-started
j
I tried
var current: Body? = this@Body
but the compiler doesn't like that
🧵 2
j
You should use
this@iterator
instead (the name function that has the receiver you want to refer to)
j
oh, cool. that works but it totally doesn't match my mental model of what's going on. what should I read up on to understand better?
j
You're thinking in terms of types / class names, you should think in terms of "who provides the
this
you're trying to access".
j
that makes sense, thanks
j
You can read more about it here: https://kotlinlang.org/docs/this-expressions.html
c
I think of it as `this`as it was at
function/label
j
that's helpful, thanks
y
If you would like to access a receiver based on its type, a utility function like this works:
Copy code
fun <A> A.given(): A = this
usage:
Copy code
operator fun Body.iterator(): Iterator<Body> {
    return object : Iterator<Body> {
        var current: Body? = given<Body>()
        ...
    }
}
(This really comes in handy with Context Receivers because it allows differentiating between a
List<Int>
and a
List<String>
for instance)