Struct-typing in Phobos

rumbu rumbu at rumbu.ro
Thu Feb 6 14:20:06 PST 2014


I think the most elegant way to satisfy both crowds (OOP and 
anti-OOP) is to use something like this:

https://gist.github.com/rjmcguire/6431542

void foo(T)(T someSet) if (Implements!(T, Set))
{
}

foo will accept any class implementing interface "Set", any 
struct having the same methods as the interface "Set", and any 
other type having equivalent UFCS for "Set" members.

So, the OOP crowd will write classes implementing Set, the 
anti-OOP crowd will create structs and finally, the hardcore 
programming crowd will be happy using UFCS. Everybody is happy.

Of course, it would be nice if the compiler is doing this behind 
the scenes by accepting interface implementation for structs so I 
can write void foo(Set someSet) for any class, interface or 
struct implementing Set. C# is doing this by boxing the 
struct/value type, but I think the D compiler could be smarter 
than that.


More information about the Digitalmars-d mailing list