Pair and Triple Compare w/ Unit Tests (#2359)

* Pair and Triple Compare w/ Unit Tests
* scope creep myself by adding date-time eq op
* make Pair and Triple printable
* Update releasenotes. Restrict equals on tuples to when underlying type supports `==`. Remove unnecessary Time.eq.

---------

Co-authored-by: Christoffer Lerno <christoffer@aegik.com>
This commit is contained in:
Zack Puhl
2025-08-02 17:11:27 -04:00
committed by GitHub
parent e707190539
commit 2a47cc2ca9
7 changed files with 160 additions and 3 deletions

View File

@@ -1,11 +1,17 @@
module std::collections::pair{Type1, Type2};
import std::io;
struct Pair
struct Pair (Printable)
{
Type1 first;
Type2 second;
}
fn usz? Pair.to_format(&self, Formatter* f) @dynamic
{
return f.printf("{ %s, %s }", self.first, self.second);
}
<*
@param [&out] a
@param [&out] b
@@ -18,15 +24,27 @@ macro void Pair.unpack(&self, a, b)
*b = self.second;
}
module std::collections::triple{Type1, Type2, Type3};
fn bool Pair.equal(self, Pair other) @operator(==) @if (types::has_equals(Type1) &&& types::has_equals(Type2))
{
return self.first == other.first && self.second == other.second;
}
struct Triple
module std::collections::triple{Type1, Type2, Type3};
import std::io;
struct Triple (Printable)
{
Type1 first;
Type2 second;
Type3 third;
}
fn usz? Triple.to_format(&self, Formatter* f) @dynamic
{
return f.printf("{ %s, %s, %s }", self.first, self.second, self.third);
}
<*
@param [&out] a
@param [&out] b
@@ -42,6 +60,12 @@ macro void Triple.unpack(&self, a, b, c)
*c = self.third;
}
fn bool Triple.equal(self, Triple other) @operator(==) @if (types::has_equals(Type1) &&& types::has_equals(Type2) &&& types::has_equals(Type3))
{
return self.first == other.first && self.second == other.second && self.third == other.third;
}
module std::collections::tuple{Type1, Type2};
struct Tuple @deprecated("Use 'Pair' instead")

View File

@@ -339,6 +339,8 @@ macro bool is_same_vector_type($Type1, $Type2) @const
$endif
}
macro bool has_equals($Type) @const => $defined(($Type){} == ($Type){});
macro bool is_equatable_type($Type) @const
{
$if $defined($Type.less) || $defined($Type.compare_to) || $defined($Type.equals):

View File

@@ -112,6 +112,11 @@ fn TzDateTime TzDateTime.to_gmt_offset(self, int gmt_offset) {
return { dt, gmt_offset };
}
fn bool TzDateTime.eq(self, TzDateTime other) @operator(==) @inline
{
return self.to_gmt_offset(0).time == other.to_gmt_offset(0).time;
}
<*
@require day >= 1 && day < 32
@require hour >= 0 && hour < 24
@@ -257,3 +262,5 @@ fn Duration DateTime.diff_us(self, DateTime from) @operator(-)
{
return self.time.diff_us(from.time);
}
fn bool DateTime.eq(self, DateTime other) @operator(==) @inline => self.time == other.time;