diff --git a/src/Option.mo b/src/Option.mo index 31ded976..1af5d4ed 100644 --- a/src/Option.mo +++ b/src/Option.mo @@ -130,6 +130,13 @@ module { case _ { false } }; + /// Returns true if the optional arguments are equal according to the equality function provided, otherwise returns false. + public func equal(x : ?A, y : ?A, eq : (A, A) -> Bool) : Bool = switch (x, y) { + case (null, null) { true }; + case (?x_, ?y_) { eq(x_, y_) }; + case (_, _) { false } + }; + /// Asserts that the value is not `null`; fails otherwise. /// @deprecated Option.assertSome will be removed soon; use an assert expression instead public func assertSome(x : ?Any) = switch x { diff --git a/test/Option.test.mo b/test/Option.test.mo index 2b2c0f1d..6e207684 100644 --- a/test/Option.test.mo +++ b/test/Option.test.mo @@ -1,4 +1,5 @@ import Option "mo:base/Option"; +import Int "mo:base/Int"; import Debug "mo:base/Debug"; Debug.print("Option"); @@ -263,4 +264,15 @@ do { assert (false) } } +}; + +do { + Debug.print(" equal"); + + assert (Option.equal(null, null, Int.equal)); + assert (Option.equal(?0, ?0, Int.equal)); + assert (not Option.equal(?0, ?1, Int.equal)); + assert (not Option.equal(?0, null, Int.equal)); + assert (not Option.equal(null, ?0, Int.equal)); } +