scala / bug

Scala 2 bug reports only. Please, no questions — proper bug reports only.
https://scala-lang.org
230 stars 21 forks source link

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

Open scabug opened 13 years ago

scabug commented 13 years ago

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 commented 13 years ago

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

scabug commented 13 years ago

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

scabug commented 13 years ago

@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 commented 13 years ago

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 commented 13 years ago

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

scabug commented 13 years ago

@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 commented 12 years ago

@retronym said: https://github.com/scala/scala/pull/894

scabug commented 12 years ago

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

scabug commented 11 years ago

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 commented 11 years ago

@axel22 said: Final modifiers are now retained - fixed in: https://github.com/scala/scala/pull/1201

scabug commented 11 years ago

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

Should the issue be reopened?

scabug commented 10 years ago

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

scabug commented 8 years ago

@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 https://github.com/lampepfl/dotty/pull/1155

SethTisue commented 1 year ago

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

He-Pin commented 1 year ago

refs: Kotlin has a KEEP for static too https://github.com/Kotlin/KEEP/pull/347