"static interface" for structs

Gor Gyolchanyan gor.f.gyolchanyan at gmail.com
Tue Oct 11 07:24:03 PDT 2011


BTW, you can make a template, which takes an interface type and a
struct type and evaluates to a bool, indicating whether the struct
theoretically implements the interface.
I actually had a success in doing something similar.
Here are the main features I use in these situations:
__traits(allMembers, Type);
is(typeof(member))
__traits(getOverloads, Type);

On Tue, Oct 11, 2011 at 6:01 PM, Andrej Mitrovic
<andrej.mitrovich at gmail.com> wrote:
> On 10/11/11, Gor Gyolchanyan <gor.f.gyolchanyan at gmail.com> wrote:
>> Actually, the __traits(compiles, ...) is a marvelous and very powerful
>> tool that will allow you to test if the struct is fit for your
>> particular task by just specifying the task.
>
> Yeah, but you have to write very specific code to test if it really is
> a compatible type. And even after all that hard work you could still
> easily pass a type that "walks like a duck" but that you know is
> actually incompatible. So then you have to write specific if(!is(T ==
> IncompatibleType)), and this doesn't scale too well.
>
> This is why I resort to having an enum boolean as the first field of a
> struct that defines its "protocol", but I really see that as a
> poor-man's implementation of interfaces.
>


More information about the Digitalmars-d mailing list