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

Support @static annotation to create static fields or methods. #4581

Open
scabug opened this issue May 14, 2011 · 15 comments
Open

Support @static annotation to create static fields or methods. #4581

scabug opened this issue May 14, 2011 · 15 comments
Labels
enhancement fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/)
Milestone

Comments

@scabug
Copy link

scabug commented May 14, 2011

Having the @static annotation would fill one feature of the JVM not possible in Scala.

For practicality, it will help in these cases:

  1. Some libraries require a static field. In particular, the android SDK requires classes implementing the Parceable interface to have a CREATOR static field

  2. It will allow to create utility methods that are available "idiomatically" from Java code

I know that theoretically one can create a compiler plugin to do this. But there are few people that know how to do this.

@scabug
Copy link
Author

scabug commented May 14, 2011

Imported From: https://issues.scala-lang.org/browse/SI-4581?orig=1
Reporter: Ittay Dror (ittayd)

@scabug
Copy link
Author

scabug commented May 14, 2011

@soc said:
I guess this would be better discussed on the mailing list first.

@scabug
Copy link
Author

scabug commented May 14, 2011

@paulp said:
The second reason is invalid; static forwarders are already generated for methods in objects. You're right that there's no way to create a static field. The history of such requests suggests the answer will be "write it in java."

@scabug
Copy link
Author

scabug commented May 15, 2011

Ittay Dror (ittayd) said:
Another area where @static will help is remove the SerialVersionUID annotation. Instead users will just write:

  @static private val serialVersionUID = 432525L

Writing it java is always an option... But having a module that has some files in Java and some in Scala sounds icky to me (esp. compilation of both).

@scabug
Copy link
Author

scabug commented May 15, 2011

Ittay Dror (ittayd) said:
BTW, the "write it in Java" argument can also be used for removing the @Native annotation...

@scabug
Copy link
Author

scabug commented May 17, 2011

@dragos said:
This needs more than a simple ticket. Please open a discussion on one of the mailing lists, and maybe followup when a clear course of action has emerged.

@scabug
Copy link
Author

scabug commented Jul 13, 2012

@retronym said:
scala/scala#894

@scabug
Copy link
Author

scabug commented Jul 13, 2012

@axel22 said:
reopening, the @static methods functionality is pending.

@scabug
Copy link
Author

scabug commented Aug 3, 2012

Chris Sachs (c9r) said:
I'm pleased to see the inclusion of the @static annotation in Scala 2.10.0-M6. I noticed that @static vals don't get marked final, which limits HotSpot optimizations and jeopardizes the fabric of the cosmos. Heads up!

object Constants {
  import scala.annotation.static
  @static val Const: Int = 0 // should generate a static final field
  @static final val FinalConst: Int = 0 // ditto
}
public class Constants {
  public static int Const;

  public static int FinalConst;

  public static {};
    Code:
       0: iconst_0      
       1: putstatic     #11                 // Field Const:I
       4: iconst_0      
       5: putstatic     #14                 // Field FinalConst:I
       8: return        

  public Constants();
    Code:
       0: aload_0       
       1: invokespecial #17                 // Method java/lang/Object."<init>":()V
       4: return        
}

@scabug
Copy link
Author

scabug commented Aug 27, 2012

@axel22 said:
Final modifiers are now retained - fixed in: scala/scala#1201

@scabug
Copy link
Author

scabug commented Mar 15, 2013

Erik Bruchez (ebruchez) said (edited on Mar 15, 2013 7:29:15 PM UTC):
For reference, it seems that this was reverted, see scala/scala#1340.

Should the issue be reopened?

@scabug
Copy link
Author

scabug commented Nov 11, 2013

Benoit Sigoure (tsuna) said:
Re-opening since this has been reverted.

@scabug
Copy link
Author

scabug commented Apr 15, 2016

@lrytz said (edited on Apr 15, 2016 7:09:43 AM UTC):
looking at the "History" tab, this ticket was closed accidentally. A new SIP is in https://github.com/scala/scala.github.com/pull/491/files, the implementation in dotty in scala/scala3#1155

@scabug scabug added this to the Backlog milestone Apr 7, 2017
@SethTisue SethTisue added the fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) label Jun 15, 2023
@SethTisue
Copy link
Member

link to Scala 3 SIP: https://docs.scala-lang.org/sips/static-members.html

@He-Pin
Copy link

He-Pin commented Jun 15, 2023

refs: Kotlin has a KEEP for static too Kotlin/KEEP#347

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/)
Projects
None yet
Development

No branches or pull requests

3 participants