Purpose of template DECLARE_HANDLE in druntime

Adam D. Ruppe destructionator at gmail.com
Fri Feb 1 01:48:14 UTC 2019

On Thursday, 31 January 2019 at 20:51:37 UTC, Andre Pany wrote:
> I noticed in druntime this template is used often:
> package template DECLARE_HANDLE(string name, base = HANDLE) {
>     mixin ("alias " ~ base.stringof ~ " " ~ name ~ ";");

If my memory is correct, it is actually for old D compatibility. 
It used to do `version(D1) typedef, else alias`.

You can see in the code right above it that there is a commented 
struct and a usage of library Typedef too. A little bit further 
up is a comment about Typedef being because of new versions of D 
deprecating the keyword.

Old typedef fit the bill quite nicely, but it got deprecated and 
removed from the language. These headers used to be usable for 
various versions of D, and the mixin string lets you use keywords 
that no longer exist and centralize it a bit.

I don't think it tries to support old D any more, but I just 
don't believe anyone has wanted to change it. Programmers don't 
like to remove clever indirection tricks after writing them... 
what if you think of some better way to do it later?!

More information about the Digitalmars-d-learn mailing list