Implicit castable structs

Steven Schveighoffer schveiguy at yahoo.com
Mon Jan 14 08:58:58 PST 2008


"Frank Benoit" wrote
> 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.

I always thought it would be cool to have inheritance for structs, not for 
virtual function overloads, but just for this implicit casting purposes, and 
inheriting final functions:

struct GtkWidget {}

struct GtkContainer : GtkWidget {}

struct GtkBin : GtkContainer {}

-Steve 





More information about the Digitalmars-d mailing list