"static interface" for structs

Andrej Mitrovic andrej.mitrovich at gmail.com
Tue Oct 11 07:01:52 PDT 2011


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