Scala Programming Language
  1. Scala Programming Language
  2. SI-4581

Support @static annotation to create static fields or methods.

    Details

      Description

      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.

        Issue Links

          Activity

          Hide
          Aleksandar Prokopec added a comment -

          reopening, the @static methods functionality is pending.

          Show
          Aleksandar Prokopec added a comment - reopening, the @static methods functionality is pending.
          Hide
          Chris Sachs added a comment -

          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        
          }
          
          Show
          Chris Sachs added a comment - 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 }
          Hide
          Aleksandar Prokopec added a comment -

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

          Show
          Aleksandar Prokopec added a comment - Final modifiers are now retained - fixed in: https://github.com/scala/scala/pull/1201
          Hide
          Erik Bruchez added a comment - - edited

          For reference, it seems that this was reverted, see https://github.com/scala/scala/pull/1340.

          Should the issue be reopened?

          Show
          Erik Bruchez added a comment - - edited For reference, it seems that this was reverted, see https://github.com/scala/scala/pull/1340 . Should the issue be reopened?
          Hide
          Benoit Sigoure added a comment -

          Re-opening since this has been reverted.

          Show
          Benoit Sigoure added a comment - Re-opening since this has been reverted.

            People

            • Assignee:
              Aleksandar Prokopec
              Reporter:
              Ittay Dror
              TracCC:
              Paul Phillips
            • Votes:
              1 Vote for this issue
              Watchers:
              8 Start watching this issue

              Dates

              • Created:
                Updated:

                Development