- Notifications
You must be signed in to change notification settings - Fork1k
Add @static SIP#491
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Add @static SIP#491
Uh oh!
There was an error while loading.Please reload this page.
Conversation
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.
Comparison
This pull request implements most of machinery needed forscala/docs.scala-lang#491Only 3-rd check is not implemented by this commit.I propose to get this in faster tofixscala#1149
This pull request implements most of machinery needed forscala/docs.scala-lang#491Only 3-rd check is not implemented by this commit.I propose to get this in faster tofixscala#1149
DarkDimius commentedMar 9, 2016
The implementation was merged into Dotty. |
| For example, classes extending `android.os.Parcelable` are required to have a static field named `CREATOR` of type `android.os.Parcelable$Creator`. | ||
| Another example is using an [`AtomicReferenceFieldUpdater`](http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/AtomicReferenceFieldUpdater.html). |
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.
s/7/8/ ?
kjsingh commentedNov 24, 2016
why can't we have static as a keyword? |
sjrd commentedNov 24, 2016
Because adding new keywords breaks existing code that used them as identifiers. Adding keywords must be donevery sparingly, only when there is no other option. |
kjsingh commentedNov 24, 2016
I guess libs doing interop with Java code will be avoiding that. |
jvican commentedNov 24, 2016
@DarkDimius is there any change you want to do here before the SIP review? |
DarkDimius commentedNov 24, 2016
Why would they? You may want some data to be seen as Java static field, and you can use |
DarkDimius commentedNov 25, 2016
@jvican, I've applied those changes now and cleared git history. Feel free to merge if it looks good. |
jvican commentedNov 25, 2016
Great changes, let's merge it for next week's discussion. |
xuwei-k commentedNov 28, 2016
adriaanm commentedNov 29, 2016
The proposal should go into more detail on:
Some relevant bugs: |
retronym commentedFeb 15, 2017 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
I'd like to see the "Compilation scheme" section fleshed out. Currently, it talks about how this would fit into the compiler pipeline, but lacks details about how things would be compiled to bytecode. Here's an example I'm interested in: packagep1;objectC {@staticvalfoo:AnyRef=C;valbar:AnyRef=C } If I translate this according to the intuitive description herein, taking a few guesses at the details, I start with: publicclassTest {publicstaticvoidmain(String[]args) {System.out.println(C$.MODULE$.foo());// null }}classC {publicstaticfinalObjectfoo;static {foo =C$.MODULE$;// initializer moved to CompanionClass.<clinit> }}finalclassC$ {publicstaticC$MODULE$;privatefinalObjectbar;publicstaticObjectfoo() {returnC.foo; }static {Objectx =C.foo;// or UNSAFE.ensureClassInitialized(classOf[C])newC$(); }privateC$() {super();MODULE$ =this;bar =this; }} Running this prints finalclassC$ {publicstaticC$MODULE$;privatefinalObjectbar;publicstaticObjectfoo() {returnC.foo; }static {newC$(); }privateC$() {super();MODULE$ =this;Objectx =C.foo;// or UNSAFE.ensureClassInitialized(classOf[C])bar =this; }} Which avoids the NPE. But in that case, triggering Both of these variations have another drawback: References to So maybe we instead should put leave all the initalization code in the module class? classC {publicstaticObjectfoo;// can't be final}finalclassC$ {publicstaticC$MODULE$;privatefinalObjectbar;publicstaticObjectfoo() {returnC.foo; }static {newC$(); }privateC$() {super();MODULE$ =this;C.foo =this;bar =this; }} This looks more promising for thread-safe initialization, and preserving semantics. But we are no unable to declare |
lrytz commentedFeb 15, 2017
We could also consider omitting the accessor methods for |
DarkDimius commentedFeb 15, 2017
classC {publicstaticObjectfoo;static {foo =C$.MODULE$; }}finalclassC$ {publicstaticC$MODULE$;privatefinalObjectbar;publicstaticObjectfoo() {returnC.foo; }static {newC$(); }privateC$() {super();MODULE$ =this;Objectx =C.foo;// or UNSAFE.ensureClassInitialized(classOf[C])bar =this; }} Is the proposed desugaring.
I guess you've meant
In case you did, the order in which initializer for foo and the super constructor of the module class is run indeed may change under the proposed sip. It's discussed herehttps://github.com/scala/scala.github.com/pull/658/files#diff-9284321b7900073f358a16a971c06881R159 , do you want me to expand this discussion?
If we always trigger initialization of Thanks for the questions, I'll expand the "Compilation scheme" section. |
retronym commentedFeb 15, 2017
Yes, it would be useful to talk about the super constructor thing explicitly,
I still think that we're introducing a new form of cycle by moving |
| ```java | ||
| {% highlight java %} | ||
| class Foo { | ||
| public static int x = 5; |
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.
shouldn't this be final?
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.
yes
Add @static SIP