Why does D not have generics?
Paul Backus
snarwin at gmail.com
Tue Jan 12 22:23:51 UTC 2021
On Tuesday, 12 January 2021 at 21:32:46 UTC, Ola Fosheim Grøstad
wrote:
> So, this is one, perhaps clumsy way of doing it:
>
[...]
>
> auto pop_second(T)(T* obj){
> auto stack = obj.as_StackConcept();
> static assert (is_StackConcept!(typeof(stack)));
> auto tmp = stack.pop();
> auto tmp2 = stack.pop();
> stack.push(tmp);
> return tmp2;
> }
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.
More information about the Digitalmars-d
mailing list