# Improvements in Custom Getters and Setters

**URL:** <https://discuss.kotlinlang.org/t/improvements-in-custom-getters-and-setters/18873>\
**Category:** Language Design\
**Created:** [August 22, 2020, 5:11am UTC](https://discuss.kotlinlang.org/t/improvements-in-custom-getters-and-setters/18873 "2020-08-22T05:11:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![joaoeudes7](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/joaoeudes7/32/7869_2.png) [@joaoeudes7](https://discuss.kotlinlang.org/u/joaoeudes7)\
**Post date:** [August 22, 2020, 5:11am UTC](https://discuss.kotlinlang.org/t/improvements-in-custom-getters-and-setters/18873/1 "2020-08-22T05:11:32Z")

</div>

I don’t know if this is the best place, but is very verbose to put getters and seters in the constructor, so it could be more readable.  
Wouldn’t it be more elegant to allow custom setters and getters this way? and continue with the idea of the primary builder

What do you think use of “override”?

```kotlin
class Fool (var field: Type) {
    override getField(): Type {
         // My custom get
         return field
    }    

    override setField(value: Type) {
         // My custom validations
         field = value
    }
}

```

---

<div class="post-metadata">

**Author:** ![wdoker](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wdoker/32/4748_2.png) [@wdoker](https://discuss.kotlinlang.org/u/wdoker)\
**Post date:** [August 22, 2020, 10:55am UTC](https://discuss.kotlinlang.org/t/improvements-in-custom-getters-and-setters/18873/2 "2020-08-22T10:55:36Z")

</div>

> [@joaoeudes7](#):
>
> Wouldn’t it be more elegant to allow custom setters and getters this way?

```auto
class Fool (prop: Type) {
    var prop: Type = prop
        get() {
            // My custom get
            return field
        }
        set(value) {
            // My custom validations
            field = value
        }
}

```

Is that elegant enough?

---

<div class="post-metadata">

**Author:** ![joaoeudes7](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/joaoeudes7/32/7869_2.png) [@joaoeudes7](https://discuss.kotlinlang.org/u/joaoeudes7)\
**Post date:** [August 23, 2020, 2:44am UTC](https://discuss.kotlinlang.org/t/improvements-in-custom-getters-and-setters/18873/3 "2020-08-23T02:44:30Z")

</div>

Yeah, but not valid logic, because not override first set of constructor and logic of set run only after setter out of constructor principal.

The only solution is:

```auto
class Fool {
    constructor(prop: Type) {
        this.prop = prop // Valid logic of setter, buts isn't elegant 
    }

    var prop: Type = aDefaultValue
        get() {
            // My custom get
            return field
        }
        set(value) {
            // My custom validations
            field = value
        }
}

```
