mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
Add Maybe.equals when inner type is equatable.
This commit is contained in:
committed by
Christoffer Lerno
parent
bd1de1e7dc
commit
5c3b637cf6
@@ -34,3 +34,12 @@ macro Type? Maybe.get(self)
|
|||||||
{
|
{
|
||||||
return self.has_value ? self.value : NOT_FOUND?;
|
return self.has_value ? self.value : NOT_FOUND?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn bool Maybe.equals(self, Maybe other) @operator(==) @if(types::is_equatable_type(Type))
|
||||||
|
{
|
||||||
|
if (self.has_value)
|
||||||
|
{
|
||||||
|
return other.has_value && equals(self.value, other.value);
|
||||||
|
}
|
||||||
|
return !other.has_value;
|
||||||
|
}
|
||||||
|
|||||||
@@ -45,6 +45,7 @@
|
|||||||
- Add `String.replace` and `String.treplace` to replace substrings within a string.
|
- Add `String.replace` and `String.treplace` to replace substrings within a string.
|
||||||
- Add `Duration * Int` and `Clock - Clock` overload.
|
- Add `Duration * Int` and `Clock - Clock` overload.
|
||||||
- Add `DateTime + Duration` overloads.
|
- Add `DateTime + Duration` overloads.
|
||||||
|
- Add `Maybe.equals` and respective `==` operator when the inner type is equatable.
|
||||||
|
|
||||||
## 0.7.1 Change list
|
## 0.7.1 Change list
|
||||||
|
|
||||||
|
|||||||
21
test/unit/stdlib/collections/maybe.c3
Normal file
21
test/unit/stdlib/collections/maybe.c3
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
module maybetest @test;
|
||||||
|
|
||||||
|
import std::collections::maybe;
|
||||||
|
import std::core::types;
|
||||||
|
|
||||||
|
alias maybeint = maybe::value{int};
|
||||||
|
|
||||||
|
struct Unequatable { int value; }
|
||||||
|
|
||||||
|
alias MaybeUnequatable = maybe::Maybe{Unequatable};
|
||||||
|
|
||||||
|
fn void test_equals() @test
|
||||||
|
{
|
||||||
|
$assert(!types::is_equatable_type(MaybeUnequatable));
|
||||||
|
assert(maybeint(1) == maybeint(1));
|
||||||
|
assert(maybeint(0) == maybeint(0));
|
||||||
|
assert(maybeint(0) != maybeint(1));
|
||||||
|
assert(maybeint(1) != maybeint(0));
|
||||||
|
assert(maybeint(0) != maybe::EMPTY{int});
|
||||||
|
assert(maybe::EMPTY{int} != maybeint(0));
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user