Order of initialization

Jeff Dyer jodyer at adobe.com
Sun Apr 20 16:42:01 PDT 2008


On 4/18/08 4:52 PM, Michael O'Brien wrote:

> I'm unsure how the class level initialization code should run:
> 
> Consider:
> 
> 
> class Circle extends Shape {
>      print("Circle")
> }
> class Shape {
>      print("Shape")
> }
> 
> 
> 
> The RI and ASC currently bomb on this due to the forward class
> reference, however, my understanding is that this is perfectly legal.
> 
> Now should the Circle initialization code run first because it is
> first in the file, or should Shape run first because it is a base class.
> 
> Should we see
> 
> Circle
> Shape
> 
> 
> or
> Shape
> Circle

Circle
Shape

[note that static initialization code must now be put in 'static {...}'
blocks]

Class initialization is still an area of open investigation, but here is how
I think it should work:

1/classes initialize in the order that they appear. Static fields get
initialized in that order too. We want to avoid subtle changes in meaning
due to implicit reordering of classes. We leave it to the programmer to
define the correct order of initialization.

2/forward type references to types defined in the current compilation unit
are allowed. As you pointed out, this is more relaxed than AS3 and the
current RI and allows more programs to load successfully.

3/class instances can be created before the class statics have been
initialized. Currently class static can be read before they are initialized
and provide the default value of the storage type. I can imagine adding read
barriers to static vars to ensure that they are initialized before use. We
could disallow the instantiation of class instances before its statics are
initalized, but that would seem to make completely safe programs illegal
while not providing any more safety than a read barrier on static
properties.

Consider this case:

  class B extends A {
    static var a
    static {
      print ("static B")
      a = new A
    }
    function B () {
      print ("new B")
    }
  }

  class A {
    static var b
    static {
      print ("static A")
      b = new B
    }
    function A () {
      print ("new A")
    }
  }

prints:

  static B
  new A
  static A
  new B
  new A   [call to super constructor]

Thoughts?

Jd




More information about the Es4-discuss mailing list