Thread overview
Purpose of template DECLARE_HANDLE in druntime
Jan 31
Seb
Feb 01
Kagamin
January 31
Hi,

I noticed in druntime this template is used often:

package template DECLARE_HANDLE(string name, base = HANDLE) {
    mixin ("alias " ~ base.stringof ~ " " ~ name ~ ";");

The disadvantage is, IDEs like IntelliJ are not able to find the symbols using this template e.g.
mixin DECLARE_HANDLE!("SC_HANDLE");

What is the benefit of this template?
Why can't we just use
alias HANDLE SC_HANDLE;

Kind regards
Andre
January 31
On 1/31/19 3:51 PM, Andre Pany wrote:
> Hi,
> 
> I noticed in druntime this template is used often:
> 
> package template DECLARE_HANDLE(string name, base = HANDLE) {
>      mixin ("alias " ~ base.stringof ~ " " ~ name ~ ";");
> 
> The disadvantage is, IDEs like IntelliJ are not able to find the symbols using this template e.g.
> mixin DECLARE_HANDLE!("SC_HANDLE");
> 
> What is the benefit of this template?
> Why can't we just use
> alias HANDLE SC_HANDLE;
> 

Most likely it's a direct translation from a C preprocessor macro.

-Steve
January 31
On Thursday, 31 January 2019 at 20:51:37 UTC, Andre Pany wrote:
> Hi,
>
> I noticed in druntime this template is used often:
>
> package template DECLARE_HANDLE(string name, base = HANDLE) {
>     mixin ("alias " ~ base.stringof ~ " " ~ name ~ ";");
>
> The disadvantage is, IDEs like IntelliJ are not able to find the symbols using this template e.g.
> mixin DECLARE_HANDLE!("SC_HANDLE");
>
> What is the benefit of this template?
> Why can't we just use
> alias HANDLE SC_HANDLE;
>
> Kind regards
> Andre

Have you tried changing it to alias and check whether the testsuite still passes?
Druntime is a bit old, so I wouldn't be too surprised if this is an old relict.
February 01
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?!
February 01
It's a strong typed handle, in C it's declared as

#ifdef STRICT
typedef void *HANDLE;
#if 0 && (_MSC_VER > 1000)
#define DECLARE_HANDLE(name) struct name##__; typedef struct name##__ *name
#else
#define DECLARE_HANDLE(name) struct name##__{int unused;}; typedef struct name##__ *name
#endif
#else
typedef PVOID HANDLE;
#define DECLARE_HANDLE(name) typedef HANDLE name
#endif
February 01
On Friday, 1 February 2019 at 07:35:34 UTC, Kagamin wrote:
> It's a strong typed handle, in C it's declared as
>
> #ifdef STRICT
> typedef void *HANDLE;
> #if 0 && (_MSC_VER > 1000)
> #define DECLARE_HANDLE(name) struct name##__; typedef struct name##__ *name
> #else
> #define DECLARE_HANDLE(name) struct name##__{int unused;}; typedef struct name##__ *name
> #endif
> #else
> typedef PVOID HANDLE;
> #define DECLARE_HANDLE(name) typedef HANDLE name
> #endif

Thanks for all answers.
I understand, there was a reason in the past, but now
it is a superfluous construct.
I will create an issue and after that checks whether I can
create a pull request.

Kind regards
André
February 26
On Friday, 1 February 2019 at 08:12:23 UTC, Andre Pany wrote:
> On Friday, 1 February 2019 at 07:35:34 UTC, Kagamin wrote:
>> It's a strong typed handle, in C it's declared as
>>
>> #ifdef STRICT
>> typedef void *HANDLE;
>> #if 0 && (_MSC_VER > 1000)
>> #define DECLARE_HANDLE(name) struct name##__; typedef struct name##__ *name
>> #else
>> #define DECLARE_HANDLE(name) struct name##__{int unused;}; typedef struct name##__ *name
>> #endif
>> #else
>> typedef PVOID HANDLE;
>> #define DECLARE_HANDLE(name) typedef HANDLE name
>> #endif
>
> Thanks for all answers.
> I understand, there was a reason in the past, but now
> it is a superfluous construct.
> I will create an issue and after that checks whether I can
> create a pull request.
>
> Kind regards
> André

Issue created
https://issues.dlang.org/show_bug.cgi?id=19702