Try swapping args to __eq__ on NotImplemented

This commit is contained in:
K. Lange 2021-03-30 17:00:23 +09:00
parent 70d5f1b2b7
commit 729220568e
4 changed files with 28 additions and 1 deletions

View File

@ -201,7 +201,18 @@ int krk_valuesEqual(KrkValue a, KrkValue b) {
krk_push(b);
KrkValue result = krk_callSimple(OBJECT_VAL(type->_eq),2,0);
if (IS_BOOLEAN(result)) return AS_BOOLEAN(result);
return 0;
if (IS_NOTIMPL(result)) goto _next;
return !krk_isFalsey(result);
}
_next:
type = krk_getType(b);
if (type && type->_eq) {
krk_push(b);
krk_push(a);
KrkValue result = krk_callSimple(OBJECT_VAL(type->_eq),2,0);
if (IS_BOOLEAN(result)) return AS_BOOLEAN(result);
return !krk_isFalsey(result);
}
return 0;

View File

@ -1025,6 +1025,7 @@ int krk_isFalsey(KrkValue value) {
case KRK_VAL_NONE: return 1;
case KRK_VAL_BOOLEAN: return !AS_BOOLEAN(value);
case KRK_VAL_INTEGER: return !AS_INTEGER(value);
case KRK_VAL_NOTIMPL: return 1;
case KRK_VAL_OBJECT: {
switch (AS_OBJECT(value)->type) {
case KRK_OBJ_STRING: return !AS_STRING(value)->codesLength;

9
test/testEqInvert.krk Normal file
View File

@ -0,0 +1,9 @@
class Foo():
def __init__(self, tag):
self.tag = tag
def __eq__(self, other):
print(self.tag,'==',other.tag)
return NotImplemented
print(Foo('a') == Foo('b'))
print(Foo('a') != Foo('b'))

View File

@ -0,0 +1,6 @@
a == b
b == a
False
a == b
b == a
True