Why does D not have generics?

Paul Backus snarwin at gmail.com
Tue Jan 12 23:10:30 UTC 2021


On Tuesday, 12 January 2021 at 22:41:03 UTC, Ola Fosheim Grøstad 
wrote:
> On Tuesday, 12 January 2021 at 22:29:09 UTC, Ola Fosheim 
> Grøstad wrote:
>> On Tuesday, 12 January 2021 at 22:23:51 UTC, Paul Backus wrote:
>>> You have written a generic function that accepts any type 
>>> (unconstrained `T`), but fails to instantiate for most of 
>>> them (e.g. `pop_second!int` would not compile). In a language 
>>> like Rust or Java with type-checked generics, the compiler 
>>> would reject this function definition.
>>
>> Hm? pop_second requires StackConcept!(T,E) ?
>
> Works fine for me:
>
> [...]

The goal is to make it produce a compile-time error given only 
the *definition* of `pop_second`--so, no main function, no unit 
tests, and no other usage code.

This is impossible to do in D (or C++) if `pop_second` is a 
template, because D (like C++) does not type-check uninstantiated 
templates.


More information about the Digitalmars-d mailing list