Constraints on object initialization:
- Objects whose initialization is not complete, should not escape
- To maintain definitional bind semantics (preventing rebind and unbind), attribute initializations, which can bind, can occur no more than once.
To these ends, the initialization order is:
- Object literal sets values.
- Starting with the superclass, attribute initializers are run, but only for attributes which are were not set by the object literal.
- Constructor is run. It may set attributes but may not bind. [This seems potentially problematic in that data structures it may build may be dependent on changes in other attributes]. It may throw an exception to reject a malformed object.
Constructors are have no arguments and are implicitly (never explicitly) called.
To achieve the first constraint:
- Attribute initializers can only reference class members lexically preceding them.
- Constructors can't access 'this'
Note that: class definitions may be in a separate compilation unit from the object literal, so an object literal cannot set attribute initializers -- this must be done by the class and its superclasses. Which in turn requires that attributes are flagged as initialized.
This approach does not allow the construction of objects from arguments which don't correspond to properties. The interpreter language does not have this, so the important of this is unclear -- thought it is widely used in Java (for example initial size of a HashMap). An approach which permits such construction by extending or modifying the above approach with factory methods was discussed, but this needs further development. Per will investigate.
Back to OpenJFX Compiler