Search code examples
javagroovyscriptingjsr223

when NOT to use Pre compilation in scripting?


I read about Pre compilation script and when you must compile script.

I wanted to know if there are any cases where Pre compilation a script will cause script to fail or result in a wrong behavior ? or Pre compilation is always the right way when running scripts?

Is there option that a script will failed pre compilation but will work without it?

Any explanation will be appreciated.


Solution

  • I found the groovy disadvantages for static compiling are mainly missing dynamic features/ dynamic method dispatch.

    I found an example for dynamic method dispatch:

    class Categorizer {
    
     void accept(String s) { println "String: '$s'" }
     void accept(Number n) { println "Number: $n" }
     void accept(Object o) { println "Object: $o" }
    
     void accept(Object... objects) {
        objects.each {
          accept(it)
        }
      }
    }
    
    new Categorizer().accept(
      "a",
      "${'b'}",
      1,
      true,
      ["c", "d", 2] as Object[]
    )