Caleb B
12/24/2025, 8:25 PM<init>()V is the initializer? Besides the fact that it's the code generated when you do a constructor call, I mean. There's some really neat stuff we could do if we could blend constructor and factory methods in the language, like letting failed initializers return null instead of throwing an exception as control flow.ephemient
12/24/2025, 8:54 PMYoussef Shoaib [MOD]
12/24/2025, 8:55 PMCaleb B
12/24/2025, 9:55 PMCompanion.invoke methods, it would be a lot less verbose. Of course that would also be resolved if static ever gets added.Youssef Shoaib [MOD]
12/24/2025, 9:57 PMcompanion proposal specifically only allows 2 operators on this companion block: invoke and of (for collection literals)Youssef Shoaib [MOD]
12/24/2025, 9:58 PMclass Foo private constructor() {
companion {
operator fun invoke(): Foo? = ...
}
}Caleb B
12/24/2025, 10:03 PMCaleb B
12/24/2025, 10:06 PMYoussef Shoaib [MOD]
12/24/2025, 10:32 PMclass Foo private constructor() {
constructor(): Foo? = ...
}
Not sure how it'd work with extension functions though.Caleb B
12/24/2025, 10:51 PMconstructor() as a shorthand for static operator fun new(): Foo, similarly to how new invokes an operator function in C++. So if static extensions were ever added, you could probably do operator fun Foo::new(): Foo.
I wonder how the Java interop would work with that, though. The JVM knows it's not actually a constructor, so it couldn't be invoked like one, which would be weird for Java users reading the source code. It does lend well to Kotlin's primary constructor model, though.Caleb B
12/24/2025, 10:54 PMval x: Int = String(), but that's actually already a thing with the Companion.invoke idiom.Caleb B
12/24/2025, 11:04 PMCaleb B
12/24/2025, 11:25 PMclass Foo private constructor() {
constructor(value: String) : this()
factory(): Foo? { ... }
// ^ literally just shorthand for:
companion { operator fun invoke(): Foo? { ... } }
}
Idk, it's just constant boilerplate in the "Java without boilerplate" language. I've found myself needing to write a nullable Companion.invoke method for every single value class I create, since the whole point is to assert they satisfy a condition.Youssef Shoaib [MOD]
12/25/2025, 2:22 AMcompanion operator fun invoke(): Foo? { ... }
i.e. companion as a modifier.
Would that be short enough for you? It's 4 words vs 1 (companion operator fun invoke vs factory). Kinda reminds me of public static void main() from Java lolCaleb B
12/25/2025, 3:49 PMDaniel Pitts
12/26/2025, 6:22 PM