www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - Static init order 0.88

reply Mike Wynn <one_mad_alien hotmail.com> writes:
The fllowing highlighs the currrent problem with static inits, Java

before the class (or any static) is referenced (how long before is
undefined)
as D is statically compiled, the issue is only relivant for classes
that have static inits that reference other classes (all the rest can
be in any order).

obvious workarround here is to use a name() property with lazy name
creation, alas then for Object and [] name will not be a const :(
however this type of layout could be used (in concept) for the D
typeinfo struct. 

---------------------------
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;
}
May 07 2004
parent reply "Walter" <newshound digitalmars.com> writes:
Static initialization within a module is done in lexical order. Between
modules, the static initializers for imported modules are guaranteed to
complete before the importer. (Circular importations generate a runtime
error.)
May 10 2004
parent reply Mike Wynn <one_mad_alien hotmail.com> writes:
On Mon, 10 May 2004 11:07:03 -0700, "Walter"
<newshound digitalmars.com> wrote:

Static initialization within a module is done in lexical order. Between
modules, the static initializers for imported modules are guaranteed to
complete before the importer. (Circular importations generate a runtime
error.)
then why does 'template typename( T : T[] )' get initialised first, before 'template typename( T : Object )' when matching typename( Foo[] ) ?? see original post.
May 11 2004
parent "Walter" <newshound digitalmars.com> writes:
"Mike Wynn" <one_mad_alien hotmail.com> wrote in message
news:ffe2a0pd6r5pf0o9kdj9v1ob78dchupler 4ax.com...
 On Mon, 10 May 2004 11:07:03 -0700, "Walter"
 <newshound digitalmars.com> wrote:

Static initialization within a module is done in lexical order. Between
modules, the static initializers for imported modules are guaranteed to
complete before the importer. (Circular importations generate a runtime
error.)
then why does 'template typename( T : T[] )' get initialised first, before 'template typename( T : Object )' when matching typename( Foo[] ) ?? see original post.
Because when instantiating templates, generated code gets added at the end.
May 17 2004