digitalmars.D.bugs - static init ordering
- one_mad_alien hotmail.com (91/91) May 06 2004 I see in the docs that static initialiser order is undefined, but this d...
I see in the docs that static initialiser order is undefined, but this does lead to some problems, Java defines that a static initialiser will be run before the Class is referenced, how long before is not defined :) this means that the only order that is important is the order relating to static initialisers that use other classes (circular refs could cause compiler error). for example I have been trying to solve the typeinfo problem with a templated set of classes thus; --------------------------- import std.c.stdio; class Foo { } template typename( T : char ) { const char[] name = "char"; } template typename( T : int ) { const char[] name = "int"; } template typename( T : Object ) { // const char[] name = T.classinfo.name; char[] name; static this() { printf( "typename(%.*s){\n", T.classinfo.name ); name = T.classinfo.name; printf( "name=%.*s\n}\n", name ); } static char[] getname() { return T.classinfo.name; } } template typename( T : T[] ) { // const char[] name = typename!(T).name; char[] name; static this() { char[] tname = typename!(T).name; name = "(" ~ tname ~ ")[]"; printf( "name=%.*s\n{\n", name ); } public static char[] getname() { return typename!(T).getname()~"[]"; } } template show(T) { void show( T t ) { TypeInfo ti; ClassInfo ci; ti = t.typeinfo; ci = t.classinfo; printf("%.*s.typeinfo = 0x%x:%.*s\n", ci.name, cast(int)cast(void*)ti, ti.toString() ); } void showclass() { TypeInfo ti; ClassInfo ci; ti = T.typeinfo; ci = T.classinfo; printf("%.*s.typeinfo = 0x%x:%.*s\n", ci.name, cast(int)cast(void*)ti, ti.toString() ); } } template named( T ) { alias typename!(T).name name; void show( T t ) { TypeInfo ti; ti = t.typeinfo; printf("%.*s.typeinfo = 0x%x:%.*s\n", name, cast(int)cast(void*)ti, ti.toString() ); } void showclass() { TypeInfo ti; ti = T.typeinfo; printf("%.*s.typeinfo = 0x%x:%.*s\n", name, cast(int)cast(void*)ti, ti.toString() ); } } int main( char[][] args ) { Foo f = new Foo(); show!(Foo).showclass(); show!(Foo).show( f ); named!(int).showclass(); named!(int).show( 1 ); printf( "typename!(Foo[]).getname=%.*s\n", typename!(Foo[]).getname ); printf( "typename!(Foo[][]).getname=%.*s\n", typename!(Foo[][]).getname ); named!(Foo[]).showclass(); // named!(Foo[][]).showclass(); // named!(int[]).showclass(); // named!(int[]).show( new int[4] ); return 0; } ----------------------------- even though template typename(T:Object) is defined in the file before typename(T:T[]) typename(T:Object).name is not set when used from typename(T:T[]) obvious workaround is lazy property name() { if (_name) return _name; return _name = typename(T).getname~"[]"; } private char[]_name;
May 06 2004