Do we need to enhance @static?


#1

We know that @static was proposed in SIP 25, and it has been implemented in Dotty.
Currently, when an object member is all annotated with @static, the compiler still generates a corresponding class file for it.These objects have no practical use in most cases, and I think we should allow users to somehow prevent the compiler from generating them.For example, we can add new usage for @static:

class Test 

@static
object Test {
  @static
  def f: Int = 100
}

Or we can go one step further. When an object is annotated with @static , all its members are treated as being annotated with @static , which may help us reduce a lot of duplicate work.
Preventing the compiler from generating useless objects can help us reduce the output file size, make Dotty-generated bytecode more controllable, and be more friendly to other JVM languages.


#2

No one is interested in this proposal?
Allowing annotation of objects as @static has the additional advantage of being able to avoid object inheritance from the Any to redundant methods,.


#3

emmm that’s a good idea.


#4

I see value in such a proposal, thanks @Glavo. The static SIP in Scala 2 is for now stalled. It you’re interested in it, I’d encourage you to take ownership of the sip, make the modification in the proposal and try to implement it. You may also want to talk to @dotta and @dragos who had the static SIP implementation on their radar.


#5

What happens when you javap the classfile? That file may be needed for Scala signatures the same way a standalone object produces two classfiles

scalac a.scala
➜  cls git:(pretty) ✗ cat a.scala
object a
➜  cls git:(pretty) ✗ tree
.
├── a$.class
├── a.class
└── a.scala

0 directories, 3 files
$ javap -v a.class
Classfile /Users/ollie/dev/scalameta/target/cls/a.class
  Last modified Aug 10, 2018; size 302 bytes
  MD5 checksum 68b86ef8d96bb3c43f6d72741bd587a2
  Compiled from "a.scala"
public final class a
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_FINAL, ACC_SUPER
Constant pool:
   #1 = Utf8               a
   #2 = Class              #1             // a
   #3 = Utf8               java/lang/Object
   #4 = Class              #3             // java/lang/Object
   #5 = Utf8               a.scala
   #6 = Utf8               Lscala/reflect/ScalaSignature;
   #7 = Utf8               bytes
   #8 = Utf8               M9Q!\t\t!9A(Z7qift4\t\riA\tA\t!CNqA\t9i)1oY1mC&q:L(+4\t
                                                                                  E9A\n\rqJg.?))
   #9 = Utf8               SourceFile
  #10 = Utf8               RuntimeVisibleAnnotations
  #11 = Utf8               ScalaSig
{
}
SourceFile: "a.scala"
RuntimeVisibleAnnotations:
  0: #6(#7=s#8)
Error: unknown attribute
  ScalaSig: length = 0x3
   05 00 00