Skip to content
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

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open

Kotlin statics and static extensions #347

wants to merge 18 commits into from

Conversation

elizarov
Copy link
Contributor

This is the initial proposal for Kotlin statics and static extensions

@valery1707
Copy link

Missing character ` in heading of last table:

  • Actual:
    | Alternative  | Scheme                  | `Color.background` on JVM | Color.parse` on JVM | `Box.of` on JVM |
    
  • Fixed:
    | Alternative  | Scheme                  | `Color.background` on JVM | `Color.parse` on JVM | `Box.of` on JVM |
    

proposals/statics.md Outdated Show resolved Hide resolved
proposals/statics.md Outdated Show resolved Hide resolved
proposals/statics.md Outdated Show resolved Hide resolved
proposals/statics.md Outdated Show resolved Hide resolved
proposals/statics.md Outdated Show resolved Hide resolved
proposals/statics.md Outdated Show resolved Hide resolved
proposals/statics.md Outdated Show resolved Hide resolved
}
```

> This means, that removing a `static` modifier to an object declaration is not a source-compatible change,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
> 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
Copy link
Member

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()

proposals/statics.md Show resolved Hide resolved
Comment on lines +1325 to +1328
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.
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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.

elizarov and others added 9 commits April 3, 2023 11:20
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()`.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants