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

[stdlib] List.__eq__ and List.__ne__ for Rhs_t:ComparableCollectionElement #2702

Open
wants to merge 1 commit into
base: nightly
Choose a base branch
from

Conversation

rd4com
Copy link

@rd4com rd4com commented May 17, 2024

Hello,

here is an implementation of List.__eq__ and List.__ne__ that makes it possible to do:

var x = List(1,2,3)
var y = List(1,2,3)
if x == y: print("same values")
if x != y: print("not same values")

 

One of the downside is that part of the implementation is done with constrained,

this is not ideal because users won't be aware of some errors until m̀ojo build main.mojo shows the constrained failed.

But it is necessary to have the constrained because rhs could be a list[Bool] and self a list[Int], for example.

@rd4com rd4com requested review from a team as code owners May 17, 2024 09:22
docs/changelog.md Outdated Show resolved Hide resolved
stdlib/test/collections/test_list.mojo Outdated Show resolved Hide resolved
var d = List[Int](0, 1, 2, 3)
assert_true(a == b)
assert_false(a == c)
assert_false(a == d)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert_false(a == d)
assert_false(a == d)
assert_not_equal(a, d)

Copy link
Author

@rd4com rd4com May 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert_not_equal require the arguments to be Testable which includes EqualityComparable,

this would not work because List is not EqualityComparable and Stringable;

What the __eq__ implementation does is to exist if T of a specific list is ComparableCollectionElement.

 

(see fn __str__[U: RepresentableCollectionElement](self: List[U]) -> String: of List)

It would require the "conditional conformance"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the info! I'm assuming that at some stage assert_equal will work for collections? I wonder If it's worth (in a separate MR) attempting to enhance the testing library to work for collections that contain ComparableCollectionElement?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, @ChristopherLR , if you think it's a good idea you can always develop it

@rd4com
Copy link
Author

rd4com commented May 17, 2024

Do not accept the PR please even if test passes,

@soraros pointed out that rebind should not work with memory-only types!

@rd4com rd4com changed the title [stdlib] List.__eq__ and List.__ne__ parametrized on inferred RHS_T: ComparableCollectionElement [stdlib] List.__eq__ and List.__ne__ for Rhs_t:ComparableCollectionElement May 20, 2024
@JoeLoser JoeLoser self-assigned this May 21, 2024
@JoeLoser
Copy link
Collaborator

Do not accept the PR please even if test passes,

@soraros pointed out that rebind should not work with memory-only types!

@soraros do you mind elaborating given the test case with a memory only type? @Mogball do you have any suggestions in not using rebind here and other places we're trying to implement these sort of things without conditional conformance?

@rd4com
Copy link
Author

rd4com commented May 22, 2024

When @soraros offered that review on another PR, rebind was used on value:T(CollectionElement),
now, rebind is used on the Reference[T], which is register_passable (not memory only).

This is also why there is a: [stdlib] Fix Tuple.__contains__ PR (#2782 )

see VariadicPack.get_element:

@JoeLoser
Copy link
Collaborator

When @soraros offered that review on another PR, rebind was used on value:T(CollectionElement), now, rebind is used on the Reference[T], which is register_passable (not memory only).

This is also why there is a: [stdlib] Fix Tuple.__contains__ PR (#2782 )

see VariadicPack.get_element:

Makes sense, thanks for clarifying! So, if I understand correctly, this PR should be ready to go then, yeah? If so, do you mind resolving the merge conflicts?

@Mogball
Copy link
Collaborator

Mogball commented May 22, 2024

Do not accept the PR please even if test passes,
@soraros pointed out that rebind should not work with memory-only types!

@soraros do you mind elaborating given the test case with a memory only type? @Mogball do you have any suggestions in not using rebind here and other places we're trying to implement these sort of things without conditional conformance?

My suggestion is to never use rebind. We should rename it to __rebind

@rd4com
Copy link
Author

rd4com commented May 23, 2024

I'm happy to follow @Mogball 's expert analysis,
let's just close the PR and revisit the idea once new features lands,
i would not be comfortable forming a reference out of an bitcasted UnsafePointer,
here is a suggestion for the renaming of rebind: UnsafeRebind 👍 (ping to @Mogball)
@JoeLoser can i close this PR ?


@always_inline
fn __eq__[
inferred Rhs_t: ComparableCollectionElement
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question Do we need ComparableCollectionElement or would EqualityCollectionElement work instead (CollectionElement + EqualityComparable)?

assert_false(a == c)
assert_false(a == d)

var e = List[StringLiteral]("Mojo", "Is", "Awesome")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion Want to use alias instead here for the variables and we can make sure eq and ne work in comp-time?

Signed-off-by: rd4com <144297616+rd4com@users.noreply.github.com>
@rd4com
Copy link
Author

rd4com commented May 30, 2024

@JoeLoser , did my best to implement both reviews, the tests do not passes

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

Successfully merging this pull request may close these issues.

None yet

4 participants