Skip to content

Commit 6b7f42a

Browse files
committed
py.Bool eq and ne
1 parent 5f5112a commit 6b7f42a

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

py/bool.go

+43
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,46 @@ func (a Bool) M__str__() Object {
4646
var _ I__bool__ = Bool(false)
4747
var _ I__index__ = Bool(false)
4848
var _ I__str__ = Bool(false)
49+
50+
// Convert an Object to an Bool
51+
//
52+
// Retrurns ok as to whether the conversion worked or not
53+
func convertToBool(other Object) (Bool, bool) {
54+
switch b := other.(type) {
55+
case Bool:
56+
return b, true
57+
case Int:
58+
switch b {
59+
case 0:
60+
return False, true
61+
case 1:
62+
return True, true
63+
default:
64+
return False, false
65+
}
66+
case Float:
67+
switch b {
68+
case 0:
69+
return False, true
70+
case 1:
71+
return True, true
72+
default:
73+
return False, false
74+
}
75+
}
76+
return False, false
77+
}
78+
79+
func (a Bool) M__eq__(other Object) Object {
80+
if b, ok := convertToBool(other); ok {
81+
return NewBool(a == b)
82+
}
83+
return False
84+
}
85+
86+
func (a Bool) M__ne__(other Object) Object {
87+
if b, ok := convertToBool(other); ok {
88+
return NewBool(a != b)
89+
}
90+
return True
91+
}

0 commit comments

Comments
 (0)