-
Notifications
You must be signed in to change notification settings - Fork 363
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Kotlin statics and static extensions #347
base: master
Are you sure you want to change the base?
Conversation
Missing character
|
} | ||
``` | ||
|
||
> This means, that removing a `static` modifier to an object declaration is not a source-compatible change, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
> This means, that removing a `static` modifier to an object declaration is not a source-compatible change, | |
> This means, that removing a `static` modifier from an object declaration is not a source-compatible change, |
With such a declaration, any code in the scope of `Widget` can refer to the widget's background color as | ||
`Color.background`, which creates a DSL for uniform access to all colors via `Color.<xxx>` references in code. | ||
|
||
### Static extensions vs extensions as static members |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are "static extensions as static members" disallowed or just the corresponding section is omitted?
E.g static fun Int.static.ext()
Note, that similarly to Kotlin file-classes (class `XxxKt` that is produced when compiling top-level declaration | ||
from Kotlin file `Xxx.kt`), we will not generate private constructor in those utility classes for static objects. | ||
So, it would be possible to create an instance of static object class from Java, but the instance will be of no | ||
practical use, as its methods are static and the type of class itself is not used anywhere. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
File classes do not have any constructor, and it is not possible to instantiate them from Java despite the IDE thinking otherwise.
Despite that (file classes not having a constructor at all) it looks like an omission rather than a deliberate decision, so IMO it would be better not to replicate it in static objects, and generate an explicit private constructor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They do have a constructor and can be, in fact, instantiated from Java (albeit IDEA gives a warning "Instantiation of utility class"). Every JVM class has a "default" constructor, unless a constructor is explicitly specified.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They don't. I've checked.
Co-authored-by: Lukellmann <47486203+Lukellmann@users.noreply.github.com>
Co-authored-by: Lukellmann <47486203+Lukellmann@users.noreply.github.com>
Co-authored-by: ilya-g <ilya.gorbunov@jetbrains.com>
* Statics and the future role of companion objects * When to use static members or static extensions
**Rust** distinguishes static and instance members by the presence of the first `&self`/`self` parameter in a method | ||
declaration. Calls to static members in Rust are statically dispatched using `ClassName::method()` syntax. | ||
Static methods of Rust traits are dispatched dynamically. | ||
There are no statically dispatched trait members in Rust. There is no call-site syntax of `TraitName::method()`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is. This code compiles without issues:
trait Read {
fn read() -> Self;
}
struct X;
impl Read for X {
fn read() -> X {
X
}
}
struct Y;
impl Read for Y {
fn read() -> Y {
Y
}
}
fn main() {
let x: X = Read::read();
}
Fix a typo
This is the initial proposal for Kotlin statics and static extensions