Thread overview
Get aliased type
Jan 02, 2018
John Chapman
Jan 02, 2018
David Nadlinger
Jan 02, 2018
John Chapman
Jan 02, 2018
Jonathan M Davis
January 02, 2018
Because an alias of a type is just another name for the same thing you can't test if they're different. I wondered if there was a way to get the aliased name, perhaps via traits? (.stringof returns the original type.)

I can't use Typedef because I'm inspecting types from sources I don't control.
January 02, 2018
On Tuesday, 2 January 2018 at 11:42:49 UTC, John Chapman wrote:
> Because an alias of a type is just another name for the same thing you can't test if they're different. I wondered if there was a way to get the aliased name, perhaps via traits? (.stringof returns the original type.)

There is indeed no way to do this; as you say, aliases are just names for a particular reference to a symbol. Perhaps you don't actually need the names in your use case, though?

 — David
January 02, 2018
On Tuesday, 2 January 2018 at 12:19:19 UTC, David Nadlinger wrote:
> There is indeed no way to do this; as you say, aliases are just names for a particular reference to a symbol. Perhaps you don't actually need the names in your use case, though?
>
>  — David

The idea was to distinguish between a BSTR (an alias for wchar* from core.sys.windows.wtypes used widely with COM) and wchar* itself, chiefly so that I could call the appropriate Windows SDK functions on them to convert them to and from D strings. Although BSTRs look like wchar*s to the end user they are not really interchangable - for example, calling SysFreeString on a regular wchar* will cause a crash.

According to the docs, a BSTR is prefixed with its length and ends in a null character, but I'm not sure if checking for the existence of those is going to be good enough.
January 02, 2018
On 1/2/18 7:45 AM, John Chapman wrote:
> On Tuesday, 2 January 2018 at 12:19:19 UTC, David Nadlinger wrote:
>> There is indeed no way to do this; as you say, aliases are just names for a particular reference to a symbol. Perhaps you don't actually need the names in your use case, though?
>>
>>  — David
> 
> The idea was to distinguish between a BSTR (an alias for wchar* from core.sys.windows.wtypes used widely with COM) and wchar* itself, chiefly so that I could call the appropriate Windows SDK functions on them to convert them to and from D strings. Although BSTRs look like wchar*s to the end user they are not really interchangable - for example, calling SysFreeString on a regular wchar* will cause a crash.
> 
> According to the docs, a BSTR is prefixed with its length and ends in a null character, but I'm not sure if checking for the existence of those is going to be good enough
Hm... perhaps the correct path is to define a BSTR as a struct with a wchar in it. I wouldn't even use Typedef since they aren't the same thing (one doesn't convert to the other).

But I don't know how that would affect existing code.

-Steve
January 02, 2018
On Tuesday, January 02, 2018 11:31:28 Steven Schveighoffer via Digitalmars- d-learn wrote:
> On 1/2/18 7:45 AM, John Chapman wrote:
> > On Tuesday, 2 January 2018 at 12:19:19 UTC, David Nadlinger wrote:
> >> There is indeed no way to do this; as you say, aliases are just names for a particular reference to a symbol. Perhaps you don't actually need the names in your use case, though?
> >>
> >>  — David
> >
> > The idea was to distinguish between a BSTR (an alias for wchar* from core.sys.windows.wtypes used widely with COM) and wchar* itself, chiefly so that I could call the appropriate Windows SDK functions on them to convert them to and from D strings. Although BSTRs look like wchar*s to the end user they are not really interchangable - for example, calling SysFreeString on a regular wchar* will cause a crash.
> >
> > According to the docs, a BSTR is prefixed with its length and ends in a null character, but I'm not sure if checking for the existence of those is going to be good enough
>
> Hm... perhaps the correct path is to define a BSTR as a struct with a wchar in it. I wouldn't even use Typedef since they aren't the same thing (one doesn't convert to the other).
>
> But I don't know how that would affect existing code.

Well, regardless, if he wants to treat BSTR and wchar* as different, he's going to need to use different types for them, because aliases are literally just for the programmer's benefit and are not distinguishable via any kind of traits. So, ultimately, he either needs separate types, or he's going to have to leave it up to the programmer to deal with the differences themselves.

- Jonathan M Davis