# Kotlin function references have no identity when compiled to JS (and have different semantics on JVM and JS platforms)

**URL:** <https://discuss.kotlinlang.org/t/kotlin-function-references-have-no-identity-when-compiled-to-js-and-have-different-semantics-on-jvm-and-js-platforms/9676>\
**Category:** JavaScript\
**Created:** [October 3, 2018, 6:52pm UTC](https://discuss.kotlinlang.org/t/kotlin-function-references-have-no-identity-when-compiled-to-js-and-have-different-semantics-on-jvm-and-js-platforms/9676 "2018-10-03T18:52:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![snorbi](https://avatars.discourse-cdn.com/v4/letter/s/94ad74/32.png) [@snorbi](https://discuss.kotlinlang.org/u/snorbi)\
**Post date:** [October 3, 2018, 6:52pm UTC](https://discuss.kotlinlang.org/t/kotlin-function-references-have-no-identity-when-compiled-to-js-and-have-different-semantics-on-jvm-and-js-platforms/9676/1 "2018-10-03T18:52:04Z")

</div>

Let’s have the following code:

```
class TestClass {
    fun memberFunction() {}

    val memberFunctionReference get() = this::memberFunction
}

fun main(args: Array<String>) {
    val a = TestClass()
    println(a.memberFunctionReference == a.memberFunctionReference)
}

```

Result on JS platform: `false`  
Result on JVM platform: `true`  
(I guess that the JVM platform is always right 😉 )

The problem is with the JS translation of `memberFunctionReference()`:

```
  Object.defineProperty(TestClass.prototype, 'memberFunctionReference', {
    get: function () {
      return getCallableRef('memberFunction', function ($receiver) {
        return $receiver.memberFunction(), Unit;
      }.bind(null, this));
    }
  });

```

Ie. it always returns a new function instead of caching the result of the first invocation.

This is not a theoretical problem: when passing member function references as React component props, currently there is no way to check for their equality in `shouldComponentUpdate()`.

Maybe have you met this problem yet (and do you have a workaround 🙂 )?

---

<div class="post-metadata">

**Author:** ![Beholder](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/beholder/32/2078_2.png) [@Beholder](https://discuss.kotlinlang.org/u/Beholder)\
**Post date:** [October 3, 2018, 9:09pm UTC](https://discuss.kotlinlang.org/t/kotlin-function-references-have-no-identity-when-compiled-to-js-and-have-different-semantics-on-jvm-and-js-platforms/9676/2 "2018-10-03T21:09:10Z")

</div>

On JVM the result is not cached. There will be two different values in term of reference comparison (“`===`”), but they override `equals` method, so they equals in term of “`==`” operator.

---

<div class="post-metadata">

**Author:** ![snorbi](https://avatars.discourse-cdn.com/v4/letter/s/94ad74/32.png) [@snorbi](https://discuss.kotlinlang.org/u/snorbi)\
**Post date:** [October 3, 2018, 10:10pm UTC](https://discuss.kotlinlang.org/t/kotlin-function-references-have-no-identity-when-compiled-to-js-and-have-different-semantics-on-jvm-and-js-platforms/9676/3 "2018-10-03T22:10:05Z")

</div>

> On JVM the result is not cached

I see… but when compiling to JS, maybe caching would be a possible solution to maintain the semantics of the JVM.  
What do you think, is this a bug?

---

<div class="post-metadata">

**Author:** ![duncpro](https://avatars.discourse-cdn.com/v4/letter/d/e95f7d/32.png) [@duncpro](https://discuss.kotlinlang.org/u/duncpro)\
**Post date:** [June 27, 2023, 11:12am UTC](https://discuss.kotlinlang.org/t/kotlin-function-references-have-no-identity-when-compiled-to-js-and-have-different-semantics-on-jvm-and-js-platforms/9676/4 "2023-06-27T11:12:51Z")

</div>

I filed a bug report on the issue tracker.  
[https://youtrack.jetbrains.com/issue/KT-59666/Method-Reference-equality-is-unstable-in-Kotlin-JS](https://youtrack.jetbrains.com/issue/KT-59666/Method-Reference-equality-is-unstable-in-Kotlin-JS)
