HoriSun / closure-compiler

Automatically exported from code.google.com/p/closure-compiler
0 stars 0 forks source link

Struct extending struct produces strange warning. #1284

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Compile this example:

/**
   @constructor
   @struct
 */
function Shape() {}

/**
   @constructor
   @struct
   @extends Shape
 */
function Rectangle() {}

Rectangle.prototype = Object.create(Shape.prototype);
Rectangle.prototype.constructor = Rectangle;

2. Use the command:

java.exe -jar closure_compiler.jar --compilation_level ADVANCED_OPTIMIZATIONS 
--summary_detail_level 3 --warning_level VERBOSE --js test.js --js_output_file 
./test_result.js

What is the expected output? What do you see instead?

I expect no problems. Instead, the compiler produces:

test.js:14: WARNING - Rectangle cannot extend this type; structs can only 
extend structs
Rectangle.prototype = Object.create(Shape.prototype);
^

0 error(s), 1 warning(s), 100.0% typed

What version of the product are you using? On what operating system?

java.exe -jar closure_compiler.jar --version
Closure Compiler (http://code.google.com/closure/compiler)
Version: v20140303
Built on: 2014/03/04 10:53

Windows 7

Please provide any additional information below.

I want a struct Rectangle to extend struct Shape (e.g. because commons for all 
shapes like x,y will be defined in Shape, while width and height in Rectangle 
and radius in Circle, etc.).

However, the compiler claims that only structs can extend structs. However, 
these are both structs. So what's wrong? I can't understand the error message.

Also, what's really puzzling is why this is a warning and not an error. Does 
that mean that the line with prototype extension will be dropped, while the 
rest is compiled?

Original issue reported on code.google.com by closure....@gmail.com on 19 Mar 2014 at 3:04

GoogleCodeExporter commented 9 years ago
I have found out that when writing

Rectangle.prototype = new Shape;

it goes OK. So this syntax allowed, while Object.create style is not. I suggest 
it should be, as it's much better in many respects. The most common problem is 
when Shape ctor would require some parameters, in which case writing just 'new 
Shape' could throw, but often providing parameters could be difficult (if they 
need to constructed using several lines of code) and messy. So the 
Object.create way is much preferred. I believe it should also be supported for 
extending structs, just like the 'new' syntax.

BTW the example is taken from:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects
/Object/create

Original comment by closure....@gmail.com on 19 Mar 2014 at 3:18

GoogleCodeExporter commented 9 years ago
Closing this, as it's not related to @struct specifically. As you noticed, the 
compiler doesn't support Object.create. Please file a separate issue for that 
to be added to the type system.

Original comment by dim...@google.com on 19 Mar 2014 at 4:45