Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #916 from NilsBossung/7151
Browse files Browse the repository at this point in the history
mostly fix Issue 7151 - [CTFE] cannot compare classes with ==
  • Loading branch information
9rnsr committed Feb 6, 2015
2 parents 026a9e0 + 8d61469 commit 8c13443
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
35 changes: 32 additions & 3 deletions src/object.di
Expand Up @@ -34,7 +34,11 @@ class Object
string toString();
size_t toHash() @trusted nothrow;
int opCmp(Object o);
bool opEquals(Object o);

bool opEquals(Object o)
{
return this is o;
}

interface Monitor
{
Expand All @@ -45,8 +49,33 @@ class Object
static Object factory(string classname);
}

bool opEquals(const Object lhs, const Object rhs);
bool opEquals(Object lhs, Object rhs);
bool opEquals(const Object lhs, const Object rhs)
{
// A hack for the moment.
return opEquals(cast()lhs, cast()rhs);
}

bool opEquals(Object lhs, Object rhs)
{
// If aliased to the same object or both null => equal
if (lhs is rhs) return true;

// If either is null => non-equal
if (lhs is null || rhs is null) return false;

// If same exact type => one call to method opEquals
if (typeid(lhs) is typeid(rhs) ||
!__ctfe && typeid(lhs).opEquals(typeid(rhs)))
/* CTFE doesn't like typeid much. 'is' works, but opEquals doesn't
(issue 7147). But CTFE also guarantees that equal TypeInfos are
always identical. So, no opEquals needed during CTFE. */
{
return lhs.opEquals(rhs);
}

// General case => symmetric calls to method opEquals
return lhs.opEquals(rhs) && rhs.opEquals(lhs);
}

void setSameMutex(shared Object ownee, shared Object owner);

Expand Down
8 changes: 7 additions & 1 deletion src/object_.d
Expand Up @@ -168,8 +168,14 @@ bool opEquals(Object lhs, Object rhs)
if (lhs is null || rhs is null) return false;

// If same exact type => one call to method opEquals
if (typeid(lhs) is typeid(rhs) || typeid(lhs).opEquals(typeid(rhs)))
if (typeid(lhs) is typeid(rhs) ||
!__ctfe && typeid(lhs).opEquals(typeid(rhs)))
/* CTFE doesn't like typeid much. 'is' works, but opEquals doesn't
(issue 7147). But CTFE also guarantees that equal TypeInfos are
always identical. So, no opEquals needed during CTFE. */
{
return lhs.opEquals(rhs);
}

// General case => symmetric calls to method opEquals
return lhs.opEquals(rhs) && rhs.opEquals(lhs);
Expand Down

0 comments on commit 8c13443

Please sign in to comment.