diff --git a/core-kotlin-modules/core-kotlin-lang/src/main/kotlin/com/baeldung/operators/Page.kt b/core-kotlin-modules/core-kotlin-lang/src/main/kotlin/com/baeldung/operators/Page.kt index 1077eb94f9..8a0ee48a36 100644 --- a/core-kotlin-modules/core-kotlin-lang/src/main/kotlin/com/baeldung/operators/Page.kt +++ b/core-kotlin-modules/core-kotlin-lang/src/main/kotlin/com/baeldung/operators/Page.kt @@ -6,6 +6,7 @@ interface Page { fun elements(): MutableList } +operator fun Page.invoke(index: Int): T = elements()[index] operator fun Page.get(index: Int): T = elements()[index] operator fun Page.get(start: Int, endExclusive: Int): List = elements().subList(start, endExclusive) operator fun Page.set(index: Int, value: T) { diff --git a/core-kotlin-modules/core-kotlin-lang/src/test/kotlin/com/baeldung/operators/PageTest.kt b/core-kotlin-modules/core-kotlin-lang/src/test/kotlin/com/baeldung/operators/PageTest.kt index 4217fc0c08..fa6e1773bd 100644 --- a/core-kotlin-modules/core-kotlin-lang/src/test/kotlin/com/baeldung/operators/PageTest.kt +++ b/core-kotlin-modules/core-kotlin-lang/src/test/kotlin/com/baeldung/operators/PageTest.kt @@ -14,6 +14,11 @@ class PageTest { assertEquals(page[1, 3], listOf("Kotlin", "Scala")) } + @Test + fun `Invoke convention should work as expected`() { + assertEquals(page(1), "Kotlin") + } + @Test fun `In convention should work on a page as expected`() { assertTrue("Kotlin" in page)