Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OptionEq is broken #498

Open
mtshiba opened this issue Mar 23, 2024 · 0 comments
Open

OptionEq is broken #498

mtshiba opened this issue Mar 23, 2024 · 0 comments

Comments

@mtshiba
Copy link
Member

mtshiba commented Mar 23, 2024

Describe the behavior?

OptionEq is a patch that allows comparison of optional types. However, the current implementation is broken.

Reproducible code

C = Class { .x = Int }
C|<: Eq|.
    __eq__ self, other: C = self.x == other.x

c as C or NoneType = C.new { .x = 1 }
print! c == None

Expected result

False

Actual result

AttributeError: 'NoneType' object has no attribute 'x'

Additional context

It should be:

C|<: Eq|.
    __eq__ self, other: C = hasattr(other, "x") and self.x == other.x

But this doesn't mean the original implementation was wrong.
It is an implementation mistake that OptionEq allows T or NoneType <: Eq for T <: Eq.

To allow this, we need to inject an implementation like, for example:

OptionEq.
    __eq__ slf, other =
        classof(slf) == classof(other) and slf.__eq__ other

c as C or NoneType = C.new { .x = 1 }
print! OptionEq.__eq__ c, None

Erg version

0.6.33

Python version

None

OS

None

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant