Why does D not have generics?

Paul Backus snarwin at gmail.com
Tue Jan 12 17:32:17 UTC 2021


On Tuesday, 12 January 2021 at 17:17:40 UTC, Ola Fosheim Grøstad 
wrote:
> On Tuesday, 12 January 2021 at 17:16:19 UTC, Paul Backus wrote:
>> C++ concepts, as far as I'm aware, don't cause templates to be 
>> checked for conformance prior to instantiation--which is 
>> what's being asked for here. For that, you'd need something 
>> more like Rust's traits.
>
> Not sure what you mean? C++ use SFINAE. If it fails it will 
> look elsewhere.

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.


More information about the Digitalmars-d mailing list