https://kotlinlang.org logo
#announcements
Title
# announcements
a

Arkadii Ivanov

10/20/2019, 8:32 PM
What is the preferred naming style for a factory function? Is it better to start names from uppercase or lowercase letters? I have found the following in Kotlin Coding Conventions:
Exception: factory functions used to create instances of classes can have the same name as the class being created
But what is the recommended style?
n

Nikky

10/20/2019, 8:51 PM
as a factory function i usually put
invoke
on the companion object
a

Arkadii Ivanov

10/20/2019, 9:02 PM
Thanks but I don't really like such an approach because: 1. such overloaded
invoke
operators do not appear in auto-complete, 2. you don't always have access to a class/interface, 3. I don't like to pollute interfaces with any implementation details. Currently I prefer to put factory functions into a separate file but struggling with naming. That's why I raised this question.
k

karelpeeters

10/20/2019, 9:25 PM
If they don't do a lot of work and have functionality obvious from the signature I think having top level functions with the same name is fine. If they need a better name or do IO or something like that I'd given them better names.
3 Views