Closed myphysicslab closed 9 years ago
Using goog.forwardDeclare()
solves this issue.
When compiling ths isWithinRadius code, do you compile it with any other file that defines goog.math.Coordinate?
If not, then yes, you have to use goog.forwardDeclare. Note that in that case, the only thing that NTI knows about Coordinate is that it's defined somewhere, and nothing else. So it doesn't do typechecking on uses of Coordinate. See this example:
We're passing a number to the function that takes a Foo without a warning.
The isWithinRadius
is example code from Bolin's Closure book. In my code, yes the forward-declared file is being compiled as well. You are saying that anything forward-declared won't be type checked (in that file)?
I've refactored some of this code, a current example is:
file: AbstractEdge.js
goog.provide('myphysicslab.lab.engine2D.AbstractEdge');
goog.forwardDeclare('myphysicslab.lab.engine2D.Polygon');
goog.require('myphysicslab.lab.engine2D.Edge');
goog.require('myphysicslab.lab.engine2D.Vertex');
goog.require('myphysicslab.lab.engine2D.UtilEngine');
goog.require('myphysicslab.lab.util.UtilityCore');
goog.require('myphysicslab.lab.util.Vector');
/**
* @param {!myphysicslab.lab.engine2D.Polygon} body
* @param {!myphysicslab.lab.engine2D.Vertex} vertex1 the previous vertex, in body
coords; matches the next (second) vertex of the previous edge
* @param {!myphysicslab.lab.engine2D.Vertex} vertex2 the next vertex, in body coords
* @constructor
* @struct
* @implements {myphysicslab.lab.engine2D.Edge}
*/
myphysicslab.lab.engine2D.AbstractEdge = function(body, vertex1, vertex2) {
// code omitted...
/** the {Polygon} that this edge is a part of
* @type {!myphysicslab.lab.engine2D.Polygon}
* @protected
*/
this.body_ = body;
};
I can't really avoid circular references among these classes (well... not easily). A Polygon has Edges and it's very handy for an Edge to know which Polygon it belongs to.
It's pretty unlikely that a wrong type of object would be passed in this case so it's not that worrisome, but it's good to know what is not being type-checked.
You are saying that anything forward-declared won't be type checked (in that file)?
(I think that) if you include in the same compilation job the file that defines the forward-declared types, then you will get type checking as usual. You can try this to verify.
If you don't include the definition, then the forward-declared type name is treated as unknown.
Confirmed: I just removed all the goog.forwardDeclare()
statements and I no longer get the warning "Type annotation references non-existent type".
If you don't include the definition, then the forward-declared type name is treated as unknown.
This would only come up with modules, right? A situation where you are not compiling everything together. (A while back I tried and failed to understand how to do modules with closure-compiler).
This would only come up with modules, right?
Yes, I think so.
With new type inference
--new_type_inf
, getting the warning "Type annotation references non-existent type" on a type that has not beengoog.require
d, but is only used in a @return type declaration. Here is the error message:Here is some of that file, line 625 is the
@return
declaration.In the book Closure, The Definitive Guide by Michael Bolin, p. 50 it is stated that this should be OK:
If this is changing with new type inference, how can we deal with circular references like this?
compiler version: