| |
| Posted by Mike Wynn | PermalinkReply |
|
Mike Wynn
| The fllowing highlighs the currrent problem with static inits, Java
and C#, both have the rule that a static initialiser will be called
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;
}
|