static this sucks, we should deprecate it

Ary Borenszweig ary at esperanto.org.ar
Thu May 28 14:37:57 PDT 2009


Frank Benoit escribió:
> Unknown W. Brackets schrieb:
>> Probably a silly idea, but what about (or similar):
>>
>> static this: mod.name, mod.name2, mod.name3
>> {
>> }
>>
>> For a dependency list.  I may be wrong, but afaik the main problems stem
>> from either wrong order or co-dependence (which needs to be solved by
>> the programmer.)
>>
>> At least with this, you could ask the compiler for an order,
>> potentially.  If the other modules had no static this, it could ignore
>> it, allowing future proofing.
>>
>> But, maybe that's an ugly hack.
>>
>> -[Unknown]
>>
> 
> In Java the
> static { /* static ctor code */ }
> does not have the circular dependency problem. why is that?

Consider this:

class A {

	public static int x;

	static {
		x = B.x + 10;
	}

	public static void main(String[] args) {
		System.out.println("A.x = " + A.x);
		System.out.println("B.x = " + B.y);
	}

}

class B {

	public static int y;

	static {
		y = A.x + 10;
	}

	public static void main(String[] args) {
		System.out.println("A.x = " + A.x);
		System.out.println("B.x = " + B.y);
	}

}

If you run A, you'll get:

A.x = 20
B.x = 10

If you run B, you'll get:

A.x = 10
B.x = 20

That's because the static { } is run when the class is first loaded.

So in a sense there is the problem is circular dependency: depending on 
the order of class loading you get different results.

Is this the problem being discussed, how to define the order of static this?



More information about the Digitalmars-d mailing list