# Setter for optional property

**URL:** <https://discuss.kotlinlang.org/t/setter-for-optional-property/2372>\
**Category:** Language Design\
**Created:** [March 8, 2017, 10:35pm UTC](https://discuss.kotlinlang.org/t/setter-for-optional-property/2372 "2017-03-08T22:35:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![cuddlecheek](https://avatars.discourse-cdn.com/v4/letter/c/5f9b8f/32.png) [@cuddlecheek](https://discuss.kotlinlang.org/u/cuddlecheek)\
**Post date:** [March 8, 2017, 10:35pm UTC](https://discuss.kotlinlang.org/t/setter-for-optional-property/2372/1 "2017-03-08T22:35:36Z")

</div>

I need an optional property, but I want the setter to forbid null.

```
var myProperty: Long?
    set(newValue) {
        if (newValue != null) {
            field = someMethod(newValue)
        }
    }

```

I think Kotlin should support something like this:

```
var myProperty: Long?
    set(newValue: Long) {
        field = someMethod(newValue)
    }

```

---

<div class="post-metadata">

**Author:** ![nanodeath](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/nanodeath/32/1803_2.png) [@nanodeath](https://discuss.kotlinlang.org/u/nanodeath)\
**Post date:** [March 8, 2017, 11:08pm UTC](https://discuss.kotlinlang.org/t/setter-for-optional-property/2372/2 "2017-03-08T23:08:18Z")

</div>

This looks vaguely like something `lateinit` handles. Agree or disagree?

Edit: I mean, clearly they’re different because you can only set a `lateinit` once, IIRC. But still, I imagine the use cases overlap somewhat.

---

<div class="post-metadata">

**Author:** ![cuddlecheek](https://avatars.discourse-cdn.com/v4/letter/c/5f9b8f/32.png) [@cuddlecheek](https://discuss.kotlinlang.org/u/cuddlecheek)\
**Post date:** [March 9, 2017, 8:22am UTC](https://discuss.kotlinlang.org/t/setter-for-optional-property/2372/3 "2017-03-09T08:22:14Z")

</div>

@nanodeath  
In some cases, they overlap. Sometimes they don’t. I still need the property to be optional, because it’s completely okay if it’s null. But once it’s set to value, it shouldn’t be set to null again. If the property had to have a value every time I access it, then I could use lateinit. In my case, I can’t.

---

<div class="post-metadata">

**Author:** ![abuabdul](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abuabdul/32/2355_2.png) [@abuabdul](https://discuss.kotlinlang.org/u/abuabdul)\
**Post date:** [March 9, 2017, 8:33am UTC](https://discuss.kotlinlang.org/t/setter-for-optional-property/2372/4 "2017-03-09T08:33:51Z")

</div>

> [@cuddlecheek](#):
>
> var myProperty: Long?  
> set(newValue: Long) {  
> field = someMethod(newValue)  
> }

You cannot assign an optional long type to null safe type just like var cannot be assigned to val. Also, if you have a optional property you can safely use it without worrying about NPE, thats what null safety is designed for. Would you not want to do something like this?

```
      var myProperty: Long? = Long.MIN_VALUE
        get() = myProperty
        set(newValue) {
            field = newValue?.toLong()
        }

```

---

<div class="post-metadata">

**Author:** ![yole](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/yole/32/1425_2.png) [@yole](https://discuss.kotlinlang.org/u/yole)\
**Post date:** [March 9, 2017, 8:48am UTC](https://discuss.kotlinlang.org/t/setter-for-optional-property/2372/5 "2017-03-09T08:48:02Z")

</div>

This is covered by [https://youtrack.jetbrains.com/issue/KT-4075](https://youtrack.jetbrains.com/issue/KT-4075), which will likely be implemented at some point.

---

<div class="post-metadata">

**Author:** ![abuabdul](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abuabdul/32/2355_2.png) [@abuabdul](https://discuss.kotlinlang.org/u/abuabdul)\
**Post date:** [March 9, 2017, 9:05am UTC](https://discuss.kotlinlang.org/t/setter-for-optional-property/2372/6 "2017-03-09T09:05:05Z")

</div>

This is tracked only for setters of optional property. In Kotlin, we cannot assign a Long? to Long type. Correct?

---

<div class="post-metadata">

**Author:** ![cuddlecheek](https://avatars.discourse-cdn.com/v4/letter/c/5f9b8f/32.png) [@cuddlecheek](https://discuss.kotlinlang.org/u/cuddlecheek)\
**Post date:** [March 9, 2017, 1:04pm UTC](https://discuss.kotlinlang.org/t/setter-for-optional-property/2372/7 "2017-03-09T13:04:06Z")

</div>

I created the thread for optional properties, but it is actually about general concept. The issue linked by @yole is exactly what I meant.

---

<div class="post-metadata">

**Author:** ![umar.uzdenov.krch](https://avatars.discourse-cdn.com/v4/letter/u/35a633/32.png) [@umar.uzdenov.krch](https://discuss.kotlinlang.org/u/umar.uzdenov.krch)\
**Post date:** [March 19, 2017, 6:06pm UTC](https://discuss.kotlinlang.org/t/setter-for-optional-property/2372/8 "2017-03-19T18:06:02Z")

</div>

In this case you can use elvis operator  
set(value) { field = someMethod(value ?: return) }
