[Issue 1023] New: Struct implementing interfaces and struct member enumeration

Downs default_357-line at yahoo.de
Thu Mar 8 02:32:51 PST 2007


d-bugmail at puremagic.com schrieb:
> http://d.puremagic.com/issues/show_bug.cgi?id=1023
> 
>            Summary: Struct implementing interfaces and struct member
>                     enumeration
>            Product: D
>            Version: unspecified
>           Platform: All
>         OS/Version: All
>             Status: NEW
>           Severity: enhancement
>           Priority: P2
>          Component: DMD
>         AssignedTo: bugzilla at digitalmars.com
>         ReportedBy: larsivar at igesund.net
> 
> 
> Structs in D are very simple which is good, but in some cases they are just
> too simple. In many cases you would want to handle struct instances as if
> they were Object derivates, by calling methods on them, methods that
> implement an interface. The most obvious example is toUtf8 (or toString).
> As it is now, for instance in a formatter, if it detects that the input is
> a struct, you have no way of figuring out whether the struct implements an
> interface or not.
> 
> This is thus a request to make it possible to say that a struct implements
> an interface and thus can be typeid'ed on other types (interfaces) than
> those of the structs themselves. An enumeration of the methods of the
> struct would also be nice.
> 
> I started a thread in the NG out of old habit, but I think this is the more
> correct place.
> 
> 
Consider the following code.

import std.stdio, std.traits;

struct a { void test() { } }
struct b { void Test() { } }
struct c { int Test() { return 0; } }

void test(T)() {
   T t=void;
   static if (is(typeof(t.Test)==function)) {
     writefln(T.mangleof, " has Test, returns ", typeid(ReturnType!(t.Test)));
   }
   else writefln(T.mangleof, " lacks Test");
}

void main() {
   test!(a); test!(b); test!(c);
}

Thus, we can check if a struct implements a series of functions, and check their return values and the parameters they take.
Does that come close to what you need?
   greetings --downs


More information about the Digitalmars-d-bugs mailing list