# What are the advantages of "companion object" in properties delegating

**URL:** <https://discuss.kotlinlang.org/t/what-are-the-advantages-of-companion-object-in-properties-delegating/22895>\
**Category:** Language Design\
**Created:** [September 7, 2021, 8:46am UTC](https://discuss.kotlinlang.org/t/what-are-the-advantages-of-companion-object-in-properties-delegating/22895 "2021-09-07T08:46:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![heroo.lee](https://avatars.discourse-cdn.com/v4/letter/h/3bc359/32.png) [@heroo.lee](https://discuss.kotlinlang.org/u/heroo.lee)\
**Post date:** [September 7, 2021, 8:46am UTC](https://discuss.kotlinlang.org/t/what-are-the-advantages-of-companion-object-in-properties-delegating/22895/1 "2021-09-07T08:46:35Z")

</div>

Hi,there. I’m reading the “Programming Kotlin, By Stephen Samuel , Stefan Bocutiu”  
and I’m confused by the following code in Chatper 6-Delegated properties:

“”"  
interface WithId {  
val id: String  
}

data class WithIdImpl(override val id: String) : WithId

class Record(id: String) : WithId by Record.identifier(id) {  
companion object Record {  
fun identifier(i: String) = WithIdImpl(i)  
}  
}  
“”"

Why don’t we declare the class like this?

> class Record(id: String) : WithId by WithIdImpl(id) {}

What advantages does the former have?  
Thanks!

---

<div class="post-metadata">

**Author:** ![al3c](https://avatars.discourse-cdn.com/v4/letter/a/e47774/32.png) [@al3c](https://discuss.kotlinlang.org/u/al3c)\
**Post date:** [September 7, 2021, 11:39am UTC](https://discuss.kotlinlang.org/t/what-are-the-advantages-of-companion-object-in-properties-delegating/22895/2 "2021-09-07T11:39:30Z")

</div>

> [@heroo.lee](#):
>
> Why don’t we declare the class like this?

Here doesn’t make any difference, you’re just inlining `fun identifier`.

> [@heroo.lee](#):
>
> What advantages does the former have?

You can run some code before retuning the `WithId` object, so you could construct an implementation which requires you to compute some other object.

I can’t really tell how useful or common this pattern is, I personally never used it.

---

<div class="post-metadata">

**Author:** ![heroo.lee](https://avatars.discourse-cdn.com/v4/letter/h/3bc359/32.png) [@heroo.lee](https://discuss.kotlinlang.org/u/heroo.lee)\
**Post date:** [September 8, 2021, 1:29am UTC](https://discuss.kotlinlang.org/t/what-are-the-advantages-of-companion-object-in-properties-delegating/22895/3 "2021-09-08T01:29:19Z")

</div>

Thanks a lot.@al3c
