Open scabug opened 13 years ago
Imported From: https://issues.scala-lang.org/browse/SI-4581?orig=1 Reporter: Ittay Dror (ittayd)
@soc said: I guess this would be better discussed on the mailing list first.
@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."
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).
Ittay Dror (ittayd) said: BTW, the "write it in Java" argument can also be used for removing the @native annotation...
@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.
@retronym said: https://github.com/scala/scala/pull/894
@axel22 said: reopening, the @static methods functionality is pending.
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
}
@axel22 said: Final modifiers are now retained - fixed in: https://github.com/scala/scala/pull/1201
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?
Benoit Sigoure (tsuna) said: Re-opening since this has been reverted.
@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
link to Scala 3 SIP: https://docs.scala-lang.org/sips/static-members.html
refs: Kotlin has a KEEP for static too https://github.com/Kotlin/KEEP/pull/347
Having the @static annotation would fill one feature of the JVM not possible in Scala.
For practicality, it will help in these cases:
Some libraries require a static field. In particular, the android SDK requires classes implementing the Parceable interface to have a CREATOR static field
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.