Implicit castable structs

Frank Benoit keinfarbton at googlemail.com
Tue Jan 8 15:04:42 PST 2008


In GTK programming, there are struct that act like a object hierarchy.

struct GtkWidget{ /* data member */ }
struct GtkContainer{
	GtkWidget widget; // parent is always first member
	// more data members
}
struct GtkBin{
	GtkContainer container; // parent is always first member
	// more data members
}
extern(C) void gtk_widget_foo( GtkWidget* widget );

// in user code ...
GtkBin* bin;
gtk_widget_foo( bin ); // Error: cannot implicit cast

Can the D compiler be enhanced to allow this? More precisely:
"A pointer to a struct is implicitly convertable to all types, that map 
to the first position in the struct"

In the case above, a GtkBin* would be convertable to GtkContainer* and 
GtkWidget*. A GtkContainer* would /not/ be convertable to GtkBin* 
because it does not contain it.




More information about the Digitalmars-d mailing list