# Trailing lambda for get operator

**URL:** <https://discuss.kotlinlang.org/t/trailing-lambda-for-get-operator/31235>\
**Category:** Language Design\
**Created:** [September 26, 2026, 8:52am UTC](https://discuss.kotlinlang.org/t/trailing-lambda-for-get-operator/31235 "2026-09-26T08:52:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Landei](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/landei/32/9407_2.png) [@Landei](https://discuss.kotlinlang.org/u/Landei)\
**Post date:** [September 26, 2026, 8:52am UTC](https://discuss.kotlinlang.org/t/trailing-lambda-for-get-operator/31235/1 "2026-09-26T08:52:19Z")

</div>

I wrote some code like this:

```auto
class Cache<K, V>(val capacity: Int = 10) {
    val cache = LinkedHashMap<K,V>()

    fun invalidate(key: K) = cache.remove(key)

    operator fun get(key: K, onCacheMiss: () -> V): V =
        cache[key] ?: run {
            if (cache.size > capacity) {
                cache.remove(cache.keys.first())
            }
            onCacheMiss().also { cache[key] = it }
        }

}

```

Now, my simple brain expected that the “trailing lambda” rules would also apply to the `get` operator, but nope, I have to write:  
`cache["a", { someCalculation() }]`

instead of the much nicer and more natural  
`cache["a"]{ someCalculation() }`.

The thing is, if I change from the `get` operator to `invoke`, it works (and I’m pretty sure I have seen examples of this in the Kotlin API itself)

I know this is an edge case, but it feels like an inconsistency. What do you think?

---

<div class="post-metadata">

**Author:** ![Landei](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/landei/32/9407_2.png) [@Landei](https://discuss.kotlinlang.org/u/Landei)\
**Post date:** [September 26, 2026, 9:06am UTC](https://discuss.kotlinlang.org/t/trailing-lambda-for-get-operator/31235/2 "2026-09-26T09:06:03Z")

</div>

I found a workaround, but it’s a little mind bender:

```auto
class Cache<K, V>(val capacity: Int = 10) {
    val cache = LinkedHashMap<K, V>()

    fun invalidate(key: K) = cache.remove(key)

    operator fun get(key: K): (() -> V) -> V = { onCacheMiss: () -> V ->
        cache[key] ?: run {
            if (cache.size > capacity) {
                cache.remove(cache.keys.first())
            }
            onCacheMiss().also { cache[key] = it }
        }
    }

}

```

Now `cache["a"]{ someCalculation() }` works, `cache["a", { someCalculation() }]` doesn’t work anymore, and many people would be slightly confused how to use this mess.
