Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
533 views
in Technique[技术] by (71.8m points)

groovy - Difference between @TypeChecked and @CompileStatic

Can someone explain the difference between @TypeChecked and @CompileStatic?

I read that with @TypeChecked it is not possible to add new methods at runtime. What other features are not allowed?

Which Groovy Features are allowed with @CompileStatic? Is the bytecode same as compiled with javac in compare to groovyc and @CompileStatic?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The major difference is the MOP (Meta Object Protocol): @TypeChecked keep methods going through the MOP, while @CompileStatic generate method calls similar to Java's bytecode. This means their semantic are different, but it also means you can still apply metaprogramming on top of a @TypeChecked code, as long as the method call can be resolved at compile time.

The following code shows the MOP acting on a @TypeChecked code, and not on @CompileStatic code:

import groovy.transform.CompileStatic as CS
import groovy.transform.TypeChecked as TC

class Foo {
  def bar = "bar"
}

class TestTC {
  Foo foo

  TestTC() {
    foo = new Foo()
    foo.metaClass.getBar = { "metaClass'd bar" }
  }

  @TC
  def typed() {
    foo.bar
  }

  @CS 
  def compiled() {
    foo.bar
  }
}

assert new TestTC().typed() == "metaClass'd bar"
assert new TestTC().compiled() == "bar"

For @CompileStatic, yes, Groovy tries to generate bytecode close to what javac would output, thus, their performance are very close, with a few exceptions.


(Updated 2016-01-13)

Both @CompileStatic and @TypeChecked will allow:

  • Closures (including Closure delegations through @DelegatesTo);
  • ASTs (which can be used for compile-time metaprogramming);
  • Groovy's syntatic sugar, like those on regex, lists, maps, operator overload and the likes;
  • Extensions.

For @TypeChecked, you can also instruct the compiler to ignore some type checks through a Type Checking Extensions, allowing more flexibility. @CompileStatic also support this, but is a little more restrictive.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...