Companion object vs top level declaration

What are the benefits and issues of using a companion object vs using a top level function.

1 Like

I think it was discussed a lot of times. Global functions pollute global name space and make it hard to use auto-completion. Companion object function in most cases is much safer. I would recommend using global functions either in very small projects, or in cases you do need for them to be global.

Don’t forget that you can just have normal, non-companion objects. You can think of them as namespaces or pure static Classes or singletons or whatever you’re used to using.