I think some of the difficulty stems from the way static initialization code runs for different classes in swf based on the advanced optimizations. That approach is like #1, but also that the initialization code can run in an opimized sense at different points and not all as part of a simple sequence of static inits for all classes. Haxe also uses an approach with static initialization code blocks, but I can't recall whether it simply runs all of them in an ordered sequence after the classes are defined, or if it splits them up to run each static init in a way that corresponds to when the original source code has a first concrete class reference. It might pay to check various other projects to see what they do. This is not a new or unique problem.
I have used #2 so far, and - again, so far- have been able to address issues I faced using that approach. It is very likely to be the easier approach to figure implement in the compiler, which counts for something as well. On Tue, Nov 5, 2019 at 1:52 PM Alex Harui <[email protected]> wrote: > Hi, > > The issue of complex static initializers and dependency order keeps coming > up. In reviewing the past discussions, there were a couple of suggestions: > 1) separate static initializers into a block of code that runs after the > class is defined > 2) use getters > > I'm going to try #2. I'm not convinced #1 will work well with minifiers > or help us get the dependency order right. > > Thoughts? Is there a case where getters won't work? > > For: > > public class Foo { > public static const bar:String = > ResourceManager.getInstance().getString("baz"); > } > > We currently generate: > > Foo.bar = ResourceManager.getInstance().getString("baz"); > > And lots of other code tries to understand that the > goog.require("ResourceManager") is more important than other goog.requires > in the remove-circulars dependency calculation. > > But if we generate: > Foo.get__bar = function() { return > ResourceManager.getInstance().getString("baz");}; > Object.defineProperty(Foo, "bar", {get: Foo.get__bar}); > > Then I think no statics will get evaluated at load time. > > Of course, I could be wrong... > -Alex > >
