Re: [closure-compiler-discuss] Can't really make "@interface" work with externs

100 views
Skip to first unread message

John Lenz

unread,
May 22, 2013, 10:55:01 AM5/22/13
to closure-compiler
We have a compiler optimization option: "remove unused prototype properties in externs" that is currently enabled by default which removes "init".  If you use the Java API you can set it using CompilerOptions#setRemoveUnusedPrototypePropertiesInExterns otherwise a custom build of the compiler is required and you can tweak the CompilationLevel class.

Alternately, you can to export "init" to prevent the function from being removed.


On Wed, May 22, 2013 at 7:31 AM, <danilo...@gmail.com> wrote:
Hello,
    I've tried to make @interface / @implements work with externs, with no luck.
I searched the internet in order to understand what was wrong, but I had no luck either.

Here is a simple example:

---- a.js ----
/** @interface */
function IFace() {};
IFace.prototype.init = function() {};

/** @constructor */
function Useful(iface) {
    iface.init();
}
---- a.js ----

---- b.js ----
/**
 * @constructor
 * @implements {IFace}
 */
function Impl() {
}

Impl.prototype.init = function() {
    alert("Hi");
}

function start() {
    var ifaceImpl = new Impl();
    var useful = new Useful(ifaceImpl);
}

window['start'] = start;
---- b.js ----

I use the following command to compile:

java -jar compiler.jar --warning_level=VERBOSE --formatting PRETTY_PRINT --compilation_level ADVANCED_OPTIMIZATIONS --js b.js --externs a.js --js_output_file compiled.js

and I obtain the following result:

---- compiled.js ----
function a() {
}
window.start = function() {
  new Useful(new a)
};
---- compiled.js ----

What am I doing wrong?

Regards,
    Danilo

--
 
---
You received this message because you are subscribed to the Google Groups "Closure Compiler Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to closure-compiler-d...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

John Lenz

unread,
May 24, 2013, 12:26:41 PM5/24/13
to closure-compiler

The compiler doesn't look at the body of functions in externs so it doesn't know the connection.  Perhaps you can compile the files together and use module to keep them as separately loadable units of Code?

On May 24, 2013 12:23 AM, <danilo...@gmail.com> wrote:
The problem is that "init" is not unused.
I mean, I know that within a.js "init" is unused (but I also tried to specify the type of parameters of the Useful constructor and it didn't help), but it's used in b.js and, anyway, it should not be considered as "unused" since that defeats the whole concept of @interface.
Also exporting "init" defeats the concept of @interface.
To unsubscribe from this group and stop receiving emails from it, send an email to closure-compiler-discuss+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

John Lenz

unread,
May 24, 2013, 1:46:31 PM5/24/13
to closure-compiler

Generally externs provide type checking and prevent renaming. Exports prevent code removal. But did you try changing the compiler option?

On May 24, 2013 10:40 AM, <danilo...@gmail.com> wrote:
I thought that saying "class A @implements class B and class B is an @interface" would prevent the compiler to optimize out methods of A that are foreseen by B as an interface.
In the case of externs I would expect it "doubly" since the compiler, not knowning which use of the methods of B implemented by A is done in an extern, should not remove any of them.

My expectations were unfulfilled, but it's ok: I'll try and find some way to get around those problems.
To unsubscribe from this group and stop receiving emails from it, send an email to closure-compiler-discuss+unsubscr...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
 
---
You received this message because you are subscribed to the Google Groups "Closure Compiler Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to closure-compiler-discuss+unsub...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 
Reply all
Reply to author
Forward
0 new messages