File tree Expand file tree Collapse file tree 4 files changed +70
-0
lines changed
Expand file tree Collapse file tree 4 files changed +70
-0
lines changed Original file line number Diff line number Diff line change @@ -238,4 +238,15 @@ describe Option do
238238 Option .from!(env_user).should eq(Some [%x( whoami) .chomp])
239239 Option .from!(env_not_existing).should eq(None (String )[])
240240 end
241+
242+ it " <=>" do
243+ a = None (Int32 )[]
244+ b = Some [0 ]
245+ c = Some [1 ]
246+ d = Some [1 ]
247+
248+ (a < b).should be_true
249+ (b < c).should be_true
250+ (c == d).should be_true
251+ end
241252end
Original file line number Diff line number Diff line change @@ -236,4 +236,19 @@ describe Result do
236236 Result .from_or!(- > { ENV [" USER" ] }, false ).should eq(Ok (String , Bool )[%x( whoami) .chomp])
237237 Result .from_or!(- > { ENV [" NOT_EXISTING" ] }, false ).should eq(Err (String , Bool )[false ])
238238 end
239+
240+ it " <=>" do
241+ x1 = Ok (Int32 , String )[1 ]
242+ y1 = Err (Int32 , String )[" error" ]
243+ (x1 < y1).should be_true
244+ (y1 < x1).should be_false
245+
246+ x2 = Ok (Int32 , String )[0 ]
247+ y2 = Ok (Int32 , String )[1 ]
248+ (x2 < y2).should be_true
249+
250+ x3 = Err (Int32 , Int32 )[0 ]
251+ y3 = Err (Int32 , Int32 )[1 ]
252+ (x3 < y3).should be_true
253+ end
239254end
Original file line number Diff line number Diff line change @@ -5,6 +5,8 @@ abstract struct Option(T)
55
66 class UnwrapNone < Exception ; end
77
8+ class Uncomparable < Exception ; end
9+
810 macro inherited
911 {% type = @type .name(generic_args: false ) % }
1012
@@ -287,6 +289,25 @@ abstract struct Option(T)
287289 rescue
288290 None (T ).new
289291 end
292+
293+ include Comparable (self )
294+
295+ def <=> (other : Option (T )) : Int32
296+ case {self , other}
297+ when {Some , None }
298+ 1
299+ when {None , Some }
300+ -1
301+ when {None , None }
302+ 0
303+ else
304+ {% if T .has_method?(:<=> ) % }
305+ self .unwrap <=> other.unwrap
306+ {% else % }
307+ 0
308+ {% end % }
309+ end
310+ end
290311end
291312
292313struct Some (T ) < Option (T )
Original file line number Diff line number Diff line change @@ -5,6 +5,8 @@ abstract struct Result(T, E)
55
66 class UnwrapErrOnOk < Exception ; end
77
8+ class Uncomparable < Exception ; end
9+
810 macro inherited
911 {% type = @type .name(generic_args: false ) % }
1012
@@ -286,6 +288,27 @@ abstract struct Result(T, E)
286288 rescue
287289 Err (T , E ).new(error)
288290 end
291+
292+ include Comparable (self )
293+
294+ def <=> (other : Result (T , E )) : Int32
295+ case {self , other}
296+ when {Ok , Err }
297+ -1
298+ when {Err , Ok }
299+ 1
300+ else
301+ {% if T .has_method?(:<=> ) && E .has_method?(:<=> ) % }
302+ if self .is_a?(Ok )
303+ self .unwrap <=> other.unwrap
304+ else
305+ self .unwrap_err <=> other.unwrap_err
306+ end
307+ {% else % }
308+ 0
309+ {% end % }
310+ end
311+ end
289312end
290313
291314struct Ok (T , E ) < Result (T , E )
You can’t perform that action at this time.
0 commit comments