static this sucks, we should deprecate it

Ary Borenszweig ary at esperanto.org.ar
Thu May 28 18:41:01 PDT 2009


Steven Schveighoffer escribió:
> On Thu, 28 May 2009 17:37:57 -0400, Ary Borenszweig 
> <ary at esperanto.org.ar> wrote:
> 
>> 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?
> 
> 
> Yes.
> 
> So Java basically allows the circular dependency because it assumes you 
> know what you are doing :)  That could be even worse than the D solution!
> 
> However, I think this could be solved in Java more readily than it could 
> be in D...

The thing is, I've been using Java for more than 7 years now and I never 
got any error because of intialization dependency. It would be nice to 
turn that check off in D and see the results. Maybe no one will complain 
about it.

(Or maybe it was added because someone complained about it, some years 
ago? I don't know...)



More information about the Digitalmars-d mailing list