Companion blocks and extensions #467
Replies: 11 comments 28 replies
-
There are some use cases where the companion is used as a namespace for imports like |
Beta Was this translation helpful? Give feedback.
-
I feel like this list should be extended. |
Beta Was this translation helpful? Give feedback.
-
Was there a specific rationale not to support companion blocks inside objects? I don't have a use-case specifically in mind but an object would still have a classifier on which you could desire companion functions/properties without wanting to resort to platform-specific annotations such as Would companion extensions still be applicable on an object classifier? |
Beta Was this translation helpful? Give feedback.
-
|
I like the KEEP, i have a question regarding:
does that mean that we will not be able to have companion extensions for classes that have static fields? e.g BigDecimal? |
Beta Was this translation helpful? Give feedback.
-
Although the full init block is not available, almost all of its usages can be emulated with class Foo {
companion {
val foo = run {
// ...
}
}
}On the codebases I work on, this covers all the cases where a Java static init block or Kotlin companion object init block are used. Still, I'm curious what is the reason for not allowing an init block there, I don't see a technical blocker? |
Beta Was this translation helpful? Give feedback.
-
|
To expand on the migration section, would this be a safe migration path? Initial: class A {
companion object {
@JvmStatic
fun foo() = 7
}
}First step, move static declaration to companion block: class A {
companion {
fun foo() = 7
}
@Deprecated("...")
companion object {
@Deprecated("...", ReplaceWith("A.foo()`))
fun foo() = A.foo()
}
}and then the usual warning→error→hidden→removal cycle? If so, library authors that don't have a permanent binary compatibility guarantee can probably do the migration over time. This is probably too breaking a change for the standard library, but it looks doable for Ktor 4 or other libraries. |
Beta Was this translation helpful? Give feedback.
-
|
What were the reasons why |
Beta Was this translation helpful? Give feedback.
-
|
After so many iterations, I can say that I like the proposal a lot. It is obvious that there was a lot of effort to conduct research, and, more importantly, explain its results. The resulting design is very concise and simple. And it does not block further development of new features. |
Beta Was this translation helpful? Give feedback.
-
|
The new design looks nice and elegantly solves // 1
object GitSchemas {
val HTTPS = "https://"
val SSH = "ssh://"
}
// 2
class GitSchemas private constructor() {
companion {
val HTTPS = "https://"
val SSH = "ssh://"
}
}(1) is more aesthetically pleasing, shorter, and less indented. Yet, (2) is more correct: you get rid of unnecessary object allocation and non-static property calls. Sadly, it gives so much There's a struggle with picking a proper modifier for this, though. First version of the proposal used companion GitSchemas { // companion to what? package? file? anything?
val HTTPS = "https://"
val SSH = "ssh://"
}There were many suggestions to have |
Beta Was this translation helpful? Give feedback.
-
|
Very minor suggestion: I think it'd be nice to have something similar here. This feels rather natural:
In other words, I propose |
Beta Was this translation helpful? Give feedback.
-
|
It's very useful to generate serializer code in KSP, and we can call Data.serializer directly. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
This is an issue to discuss companion blocks and extensions. The current full text of the proposal can be found here.
TL;DR (from the text of the proposal)
Beta Was this translation helpful? Give feedback.
All reactions