@@ -7,8 +7,8 @@ module Rs
77 # https://doc.rust-lang.org/src/core/option.rs.html
88 class Option
99 class TypeError < StandardError
10- def initialize ( type )
11- super ( "Expected #{ type } to be a Class" )
10+ def initialize ( value_type )
11+ super ( "Expected #{ value_type } to be a Class" )
1212 end
1313 end
1414
@@ -52,29 +52,31 @@ def unwrap_or_else(&block)
5252end
5353
5454class Some < Rs ::Option
55- def self . []( type , &block )
56- if !type . is_a? ( Class )
57- raise TypeError . new ( type )
55+ attr_reader :value_type
56+
57+ def self . []( value_type , &block )
58+ if !value_type . is_a? ( Class )
59+ raise TypeError . new ( value_type )
5860 end
5961
60- if type == NilClass
62+ if value_type == NilClass
6163 raise TypeNilClass . new ( "Cannot create Some[T] with NilClass" )
6264 end
6365
6466 value = block . call
65- if !value . is_a? ( type )
66- raise TypeMismatch . new ( "Expected 'Some[#{ type } ] { #{ value . inspect } }' block.call to be #{ type } , not #{ value . class } " )
67+ if !value . is_a? ( value_type )
68+ raise TypeMismatch . new ( "Expected 'Some[#{ value_type } ] { #{ value . inspect } }' block.call to be #{ value_type } , not #{ value . class } " )
6769 end
6870
6971 new ( value )
7072 end
7173
7274 def inspect
73- "Some[#{ @value . class } ] { #{ @value . inspect } }"
75+ "Some[#{ @value_type } ] { #{ @value . inspect } }"
7476 end
7577
7678 def ==( other )
77- other . is_a? ( Some ) && @value == other . unwrap && @value . class == other . unwrap . class
79+ other . is_a? ( Some ) && @value == other . unwrap && @value_type == other . value_type
7880 end
7981
8082 def initialize ( value )
@@ -83,33 +85,34 @@ def initialize(value)
8385 end
8486
8587 @value = value
88+ @value_type = value . class
8689 end
8790end
8891
8992class None < Rs ::Option
90- attr_reader :type
93+ attr_reader :value_type
9194
92- def self . []( type )
93- new ( type )
95+ def self . []( value_type )
96+ new ( value_type )
9497 end
9598
9699 def inspect
97- "None[#{ @type } ]"
100+ "None[#{ @value_type } ]"
98101 end
99102
100103 def ==( other )
101- other . is_a? ( None ) && @type == other . type
104+ other . is_a? ( None ) && @value_type == other . value_type
102105 end
103106
104- def initialize ( type = Class )
105- if !type . is_a? ( Class )
106- raise TypeError . new ( type )
107+ def initialize ( value_type = Class )
108+ if !value_type . is_a? ( Class )
109+ raise TypeError . new ( value_type )
107110 end
108111
109- if type == NilClass
112+ if value_type == NilClass
110113 raise TypeNilClass . new ( "Cannot create None[T] with NilClass" )
111114 end
112115
113- @type = type
116+ @value_type = value_type
114117 end
115118end
0 commit comments