# Contracts: Condition on "this" in extension function

**URL:** <https://discuss.kotlinlang.org/t/contracts-condition-on-this-in-extension-function/11668>\
**Category:** Support\
**Created:** [February 20, 2019, 10:06am UTC](https://discuss.kotlinlang.org/t/contracts-condition-on-this-in-extension-function/11668 "2019-02-20T10:06:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![martin.haeusler](https://avatars.discourse-cdn.com/v4/letter/m/e99b99/32.png) [@martin.haeusler](https://discuss.kotlinlang.org/u/martin.haeusler)\
**Post date:** [February 20, 2019, 10:06am UTC](https://discuss.kotlinlang.org/t/contracts-condition-on-this-in-extension-function/11668/1 "2019-02-20T10:06:03Z")

</div>

I’m trying to create an extension function like this:

```
val MyClass?.myAttribute: String?
    get() {
        if(this == null){
           return null
        }else{
           return this.performSomeOperation()
        }
    }

```

The problem with this is the following:

- I want this method to be safe when called on a `null` object (`MyClass?` is intentional)
- The method `performSomeOperation()` actually never returns `null`.
- Hence, the extension value `myAttribute` will only return `null` if `this` is `null`

In other words:

```
val str1: String = myNonNullObject.myAttribute 
val str2: String? = null.myAttribute

```

Alas, the Kotlin compiler can’t know this semantic and will therefore complain that `str1` should actually be of type `String?`.

Can I enhance the `get` function with a contract that says something like:

```
contract {
    receiverNotNull() implies (result != null)
}

```

The code above is just a dummy to outline what I’m looking for, I never used contracts so far.

---

<div class="post-metadata">

**Author:** ![fatjoe79](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fatjoe79/32/4221_2.png) [@fatjoe79](https://discuss.kotlinlang.org/u/fatjoe79)\
**Post date:** [February 21, 2019, 8:03am UTC](https://discuss.kotlinlang.org/t/contracts-condition-on-this-in-extension-function/11668/2 "2019-02-21T08:03:27Z")

</div>

You don’t need contracts for that. Just overload the function/property like this:

```
val MyClass.myAttribute: String get() = this.performSomeOperation()

val MyClass?.myAttribute: String? get() = when(this) {
    null -> null
    else -> myAttribute // the other property is taken, thanks to smart-cast
}

```

The compiler will choose the most specific property, and know its type’s nullability. That is, your example will be free of compiler errors or warnings.

---

<div class="post-metadata">

**Author:** ![martin.haeusler](https://avatars.discourse-cdn.com/v4/letter/m/e99b99/32.png) [@martin.haeusler](https://discuss.kotlinlang.org/u/martin.haeusler)\
**Post date:** [February 21, 2019, 8:23am UTC](https://discuss.kotlinlang.org/t/contracts-condition-on-this-in-extension-function/11668/3 "2019-02-21T08:23:58Z")

</div>

Right, that works too. Though I don’t see why this works: isn’t the JVM “erasure type” of both `MyClass?.myAttribute` and `MyClass.myAttribute` exactly the same? Usually the kotlin compiler complains when this happens.

---

<div class="post-metadata">

**Author:** ![fatjoe79](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fatjoe79/32/4221_2.png) [@fatjoe79](https://discuss.kotlinlang.org/u/fatjoe79)\
**Post date:** [February 21, 2019, 9:14am UTC](https://discuss.kotlinlang.org/t/contracts-condition-on-this-in-extension-function/11668/4 "2019-02-21T09:14:10Z")

</div>

There are no generics involved, so there is also no Erasure. It works for the same reason why this works:

```
fun foo(n: String) = n.toInt()
fun foo(n: Int) = n

```

Basic overloading of functions. Note that overloaded functions are statically bound at compile-time. So even if there were erasue, it would not matter, since erasure happens at runtime.

**Update:**

I see what you mean now. The JVM does not distinguish the types String? and String. So we will end-up with the same function signature twice. I don’t know how it works, but I am sure the answer is simple and can be found by decompiling the bytecode.

I know that, unlike Java, the JVM actually allows overloading methods that are different only in return type. But in our case even the return type is the same at runtime, so it must be something else.

---

<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:** [February 22, 2019, 2:17pm UTC](https://discuss.kotlinlang.org/t/contracts-condition-on-this-in-extension-function/11668/5 "2019-02-22T14:17:53Z")

</div>

You can use annotation to prevent signature clash:

```auto
val MyClass.attribute: String get() = performSomeOperation()
val MyClass?.attribute: String? @JvmName("getAttribute0") get() = this?.performSomeOperation()

```

---

<div class="post-metadata">

**Author:** ![martin.haeusler](https://avatars.discourse-cdn.com/v4/letter/m/e99b99/32.png) [@martin.haeusler](https://discuss.kotlinlang.org/u/martin.haeusler)\
**Post date:** [February 22, 2019, 2:51pm UTC](https://discuss.kotlinlang.org/t/contracts-condition-on-this-in-extension-function/11668/6 "2019-02-22T14:51:11Z")

</div>

Oh now **that’s** nice! Neat trick 🙂
