How to test for equality of types?

Matthias Walter xammy at xammy.homelinux.net
Fri May 18 14:06:00 PDT 2012



On 2012-05-18 16:12, Steven Schveighoffer wrote:
> On Fri, 18 May 2012 06:06:45 -0400, Matthias Walter wrote:
>> how do I test two types for equality? Suppose I have A and B aliasing
>> some type(s), how do I find out if they are aliases the same thing?
>>
>> I tried the "is(A == B)" expression, but this does not always work (tell
>> me if I shall give an example).
> 
> I would expect this to work.  What situation does it not (maybe you
> aren't actually testing for equality there).
> 
> It could be a bug...

=============

struct MyStruct { }

struct Wrapper(Wrap)
{
  Wrap _wrap;

  this(Wrap wrap)
  {
    _wrap = wrap;
  }
}


struct AliasStruct
{
public:
  alias MyStruct Alias;
}


int main(char[][] args)
{
  auto w = Wrapper!(AliasStruct)(AliasStruct.init);
  pragma(msg, is(Wrapper!(AliasStruct).Wrap == AliasStruct) ? "true" :
"false");
  pragma(msg, is(typeof(w._wrap) == AliasStruct) ? "true" : "false");
  pragma(msg, is(typeof(w._wrap).Alias == AliasStruct.Alias) ? "true" :
"false");


  return 0;
}
=============

prints out

false
true
false

during compilation using current git version of dmd. In my application I
used the third case, i.e., wanted to find out whether they alias the
same thing.

>> On the other hand, according to the spec the IsExpression is not
>> supposed to compare two aliases with each other.
> 
> where does it say that?

Okay, this seems to be my fault. It states different cases for the RHS
operator of "is(LHS == RHS)", e.g., "Type == TypeSpecialization", but
nothing like "Type == Type". But TypeSpecialization includes Type as well...

Best regards,

Matthias


More information about the Digitalmars-d-learn mailing list