A D vs. Rust example

Timon Gehr timon.gehr at gmx.ch
Thu Oct 27 23:18:03 UTC 2022


On 10/27/22 22:43, jmh530 wrote:
> On Thursday, 27 October 2022 at 17:37:59 UTC, Quirin Schroll wrote:
>> On Friday, 21 October 2022 at 15:13:44 UTC, Ola Fosheim Grøstad wrote:
>>> On Friday, 21 October 2022 at 13:53:28 UTC, Guillaume Piolat wrote:
>>>> But building and agreeing on abstractions is what define the ecosystem.
>>>
>>> One big weakness in C++ and D is that generic definitions cannot be 
>>> checked without instantiation.
>>
>> There aren’t many languages that have both, templates and generics; 
>> C++/CLI and C++/CX are the only ones I know and I happen to have some 
>> experience with them. (In this nomenclature: *template* = independent 
>> copy of the implementation for every distinct instantiation, cf. C++ 
>> or D templates, especially class templates with mutable static 
>> variables; *generic* = one implementation, cf. Java or C#, and static 
>> variables cannot depend on the argument types.)
>>
>> [snip]
> 
> Timon argued on the C++ pattern matching thread [1] that we need "a way 
> to parameterize functions and aggregates that is completely erased at 
> runtime (generally useful, not only for lifetimes; e.g., this is how to 
> fix `inout`.)". This bears some similarity with generics.
> 
> [1] https://forum.dlang.org/post/tj633h$1g4e$1@digitalmars.com

You can use that for generics if you allow type parameters, but I am not 
convinced that D is currently willing to add all the type system 
features (type constraints etc) and backend support [2] that would need 
to come with that to make it convenient to use. I agree that it would be 
pretty useful though (and it would enable type checking for some 
templates too). It can actually be designed to play nice with templates 
in general (which `inout` and lifetimes would also require). (If you 
instantiate a template with something that has generic arguments in it, 
implicitly add generic parameters to the template instance and 
instantiate it with the appropriate arguments.)


[2] Although interestingly, some of that is already implemented in 
typeinfo; the original non-templated built-in AA implementation was 
basically a generic type.



More information about the Digitalmars-d mailing list