class Yadriggy::UnionType
Union type. A value of this type is a value of one of the given types.
Attributes
@return [Array<Type>] the given types.
Public Class Methods
Makes an instance of {UnionType} @param [Array<Type>] ts the types included in the union type. @return [UnionType|DynType] the instance.
# 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
@param [Array<Type>] ts the types included in the union type.
# File lib/yadriggy/type.rb, line 187 def initialize(*ts) @types = ts.flatten.map {|e| UnionType.role(e)&.types || e }.flatten.uniq end
Public Instance Methods
@api private Check the subtype relation. @param [Type] t the other type. @return [Boolean] true if `self` is equivalent to `t`
or a subtype of `t`.
# File lib/yadriggy/type.rb, line 216 def <= (t) DynType == t || @types.all? {|e| e <= t } end
Checks equality. Ignores {OptionalRole} when comparing two {UnionType}s. @param [Type] t the other type. @return [Boolean] true if `self` is equivalent to `t`.
# 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
@api private
# File lib/yadriggy/type.rb, line 207 def hash @types.hash end
@param [Type] t a type. @return [Boolean] true if `self` is a super type of `t`.
# File lib/yadriggy/type.rb, line 222 def is_super_of?(t) @types.any? {|e| t <= e } end
Obtains the name of this type. @return [String] the type name.
# File lib/yadriggy/type.rb, line 228 def name name = '(' << @types.map{|e| e.name }.join('|') << ')' name end
@api private
# File lib/yadriggy/type.rb, line 202 def normalize(utype) utype.types.map {|e| e.copy(OptionalRole) } end