-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
base: nightly
Are you sure you want to change the base?
Conversation
var d = List[Int](0, 1, 2, 3) | ||
assert_true(a == b) | ||
assert_false(a == c) | ||
assert_false(a == d) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert_false(a == d) | |
assert_false(a == d) | |
assert_not_equal(a, d) |
There was a problem hiding this comment.
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"
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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
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? |
When @soraros offered that review on another PR, This is also why there is a: see mojo/stdlib/src/builtin/builtin_list.mojo Line 579 in bf73717
|
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? |
My suggestion is to never use rebind. We should rename it to |
I'm happy to follow @Mogball 's expert analysis, |
stdlib/src/collections/list.mojo
Outdated
|
||
@always_inline | ||
fn __eq__[ | ||
inferred Rhs_t: ComparableCollectionElement |
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
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>
@JoeLoser , did my best to implement both reviews, the tests do not passes |
Hello,
here is an implementation of
List.__eq__
andList.__ne__
that makes it possible to do: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 alist[Bool]
and self alist[Int]
, for example.