Class: Yadriggy::UnionType
Overview
Union type. A value of this type is a value of one of the given types.
Instance Attribute Summary collapse
-
#types ⇒ Array<Type>
readonly
The given types.
Class Method Summary collapse
-
.make(*ts) ⇒ UnionType|DynType
Makes an instance of UnionType.
Instance Method Summary collapse
-
#==(t) ⇒ Boolean
Checks equality.
-
#initialize(*ts) ⇒ UnionType
constructor
A new instance of UnionType.
-
#is_super_of?(t) ⇒ Boolean
True if
self
is a super type oft
. -
#name ⇒ String
Obtains the name of this type.
Methods inherited from Type
#!=, #copy, #eql?, error_found!, #exact_type, #has_role?, role, #supertype
Constructor Details
Instance Attribute Details
#types ⇒ Array<Type> (readonly)
Returns the given types.
167 168 169 |
# File 'lib/yadriggy/type.rb', line 167 def types @types end |
Class Method Details
.make(*ts) ⇒ UnionType|DynType
Makes an instance of Yadriggy::UnionType
172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/yadriggy/type.rb', line 172 def self.make(*ts) fts = ts.flatten fts.each do |e| return DynType if DynType == e end t = UnionType.new(fts) if t.types.size == 1 t.types[0] else t end end |
Instance Method Details
#==(t) ⇒ Boolean
Checks equality. Ignores OptionalRole when comparing two Yadriggy::UnionTypes.
195 196 197 198 199 |
# File 'lib/yadriggy/type.rb', line 195 def == (t) ut = UnionType.role(t) !ut.nil? && @types.size == ut.types.size && (normalize(self) | normalize(ut)).size <= @types.size end |
#is_super_of?(t) ⇒ Boolean
Returns true if self
is a super type of t
.
222 223 224 |
# File 'lib/yadriggy/type.rb', line 222 def is_super_of?(t) @types.any? {|e| t <= e } end |
#name ⇒ String
Obtains the name of this type.
228 229 230 231 |
# File 'lib/yadriggy/type.rb', line 228 def name name = '(' << @types.map{|e| e.name }.join('|') << ')' name end |