Joshua Hansen
08/25/2025, 10:55 PMfun main() {
val book = Book("Kotlin Basics", List(10) { Page(it + 1) })
val p = book.pages[4] // get the fullName from this scope given `book` and `p`?
}
class Book(val name: String, val pages: List<Page>) {
val Page.fullName: String
get() = "$name: Page $number of ${pages.size}"
}
class Page(val number: Int)
Youssef Shoaib [MOD]
08/25/2025, 10:57 PMfun main() = with(Book("Kotlin Basics", List(10) { Page(it + 1) })) {
val p = pages[4]
println(p.fullName)
}
class Book(val name: String, val pages: List<Page>) {
val Page.fullName: String
get() = "$name: Page $number of ${pages.size}"
}
class Page(val number: Int)
This also works with context parameters!Joshua Hansen
08/25/2025, 10:58 PMKlitos Kyriacou
08/26/2025, 8:10 AMPage.fullname
getter function has an implicit parameter (a receiver) that references the Book
but you're not using it. If you don't need such a receiver, consider defining fullname
outside of Book
or in a companion object.Youssef Shoaib [MOD]
08/26/2025, 12:35 PMname
and pages
both come from Book
.Klitos Kyriacou
08/26/2025, 5:21 PM