mojo
mojo copied to clipboard
[Feature Request] [stdlib] Add logic dunder methods for Tuple
Review Mojo's priorities
- [X] I have read the roadmap and priorities and I believe this request falls within the priorities.
What is your request?
With an iterator: Implementing an iterator and comparing per item is an option.
With pointers: I still don't quite understand how to "safely" use the unsafe pointer. I think this implementation example would have a problem when say, comparing a Tuple[UInt64, UInt64] to a Tuple[UInt8, UInt8], but I'm not sure since I barely undestand the memcmp implementation.
Could do something like:
fn _comp(self, other: Self) -> Optional[Int]:
# doesn't have an __eq__
# if self._mlir_type != other._mlir_type:
if len(self) != len(other):
return None
return memcmp(
UnsafePointer(self.storage).address,
UnsafePointer(other.storage).address,
len(self),
)
fn __eq__(self, other: Self) -> Bool:
var result = self._comp(other)
return result.value()[] == 0 if result else False
fn __ne__(self, other: Self) -> Bool:
var result = self._comp(other)
return result.value()[] != 0 if result else False
fn __gt__(self, other: Self) -> Bool:
var result = self._comp(other)
return result.value()[] == 1 if result else False
fn __ge__(self, other: Self) -> Bool:
var result = self._comp(other)
return result.value()[] != -1 if result else False
fn __lt__(self, other: Self) -> Bool:
var result = self._comp(other)
return result.value()[] == -1 if result else False
fn __le__(self, other: Self) -> Bool:
var result = self._comp(other)
return result.value()[] != 1 if result else False
What is your motivation for this change?
Some nice and intuitive syntax can come out of this
var result: Tuple[Int, UInt16, UInt8] = some_func()
if result == (0, 0, 0):
return "some thing"
return "some other thing"
Any other details?
No response
This memcmp approach won't work unfortunately, and I'm not sure the language has the features required yet to generically compare them properly.