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

Commit

Permalink
Allow mutable opEquals and show error message for migration
Browse files Browse the repository at this point in the history
  • Loading branch information
9rnsr committed Jul 9, 2012
1 parent 35c0b30 commit 67aca59
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
41 changes: 40 additions & 1 deletion import/object.di
Expand Up @@ -45,7 +45,46 @@ class Object
static Object factory(string classname);
}

bool opEquals(const Object lhs, const Object rhs);
bool opEquals(Lhs, Rhs)(Lhs lhs, Rhs rhs)
if (is(Lhs == class) && is(Rhs == class))
{
// 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) || typeid(lhs).opEquals(typeid(rhs)))
return lhs.opEquals(rhs);

static if (is(Lhs == const) || is(Rhs == const))
{
// const == const
// mutable == const
// const == mutable

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

// #1 require that parameter type is mutable Object version ?
// == calling
// ( Lhs.opEquals(Object) or Lhs.opEquals(const Object) const )
// and ( Rhs.opEquals(Object) or Rhs.opEquals(const Object) const )
return lhs.opEquals( cast(Object)(rhs) ) && rhs.opEquals( cast(Object)(lhs) );

// #2 or allow comparing between exact types
// (Allow breaking "Loskov substitution principle")
// == allow calling
// ( Lhs.opEquals(Rhs) or ... or Lhs.opEquals(const Object) const )
// and ( Rhs.opEquals(Lhs) or ... or Rhs.opEquals(const Object) const )
//return lhs.opEquals( rhs ) && rhs.opEquals( lhs );
}
}

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

Expand Down
31 changes: 28 additions & 3 deletions src/object_.d
Expand Up @@ -155,7 +155,8 @@ class Object
/************************
* Returns true if lhs and rhs are equal.
*/
bool opEquals(const Object lhs, const Object rhs)
bool opEquals(Lhs, Rhs)(Lhs lhs, Rhs rhs)
if (is(Lhs == class) && is(Rhs == class))
{
// If aliased to the same object or both null => equal
if (lhs is rhs) return true;
Expand All @@ -167,8 +168,32 @@ bool opEquals(const Object lhs, const Object rhs)
if (typeid(lhs) is typeid(rhs) || typeid(lhs).opEquals(typeid(rhs)))
return lhs.opEquals(rhs);

// General case => symmetric calls to method opEquals
return lhs.opEquals(rhs) && rhs.opEquals(lhs);
static if (is(Lhs == const) || is(Rhs == const))
{
// const == const
// mutable == const
// const == mutable

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

// #1 require that parameter type is mutable Object version ?
// == calling
// ( Lhs.opEquals(Object) or Lhs.opEquals(const Object) const )
// and ( Rhs.opEquals(Object) or Rhs.opEquals(const Object) const )
return lhs.opEquals( cast(Object)(rhs) ) && rhs.opEquals( cast(Object)(lhs) );

// #2 or allow comparing between exact types
// (Allow breaking "Loskov substitution principle")
// == allow calling
// ( Lhs.opEquals(Rhs) or ... or Lhs.opEquals(const Object) const )
// and ( Rhs.opEquals(Lhs) or ... or Rhs.opEquals(const Object) const )
//return lhs.opEquals( rhs ) && rhs.opEquals( lhs );
}
}

/**
Expand Down

0 comments on commit 67aca59

Please sign in to comment.