Why does D not have generics?

Ola Fosheim Grøstad ola.fosheim.grostad at gmail.com
Tue Jan 12 17:53:26 UTC 2021


On Tuesday, 12 January 2021 at 17:32:17 UTC, Paul Backus wrote:
> As far as I'm aware, C++ does not prevent you from writing code 
> like this:
>
> template<typename T>
> concept MyConcept = requires (T t) { T.foo(); };
>
> template<MyConcept T>
> void fun(T t) { t.bar(); }
>
> In Java or Rust, the above would be a compile-time error.

Btw, when I really want stuff like this in C++ I create a class 
with a reference to the original class. So then the interface is 
a wrapper-class and you gain access to it through an overloaded 
function with a "agreed upon name".

E.g.

someobject.protected_access.doit()

where "protected_access" is an overloaded function that returns a 
struct with a reference to someobject and the doit() interface.







More information about the Digitalmars-d mailing list