class Yadriggy::UnionType

Union type. A value of this type is a value of one of the given types.

Attributes

types[R]

@return [Array<Type>] the given types.

Public Class Methods

make(*ts) click to toggle source

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
new(*ts) click to toggle source

@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

<=(t) click to toggle source

@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
==(t) click to toggle source

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
hash() click to toggle source

@api private

# File lib/yadriggy/type.rb, line 207
def hash
  @types.hash
end
is_super_of?(t) click to toggle source

@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
name() click to toggle source

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
normalize(utype) click to toggle source

@api private

# File lib/yadriggy/type.rb, line 202
def normalize(utype)
  utype.types.map {|e| e.copy(OptionalRole) }
end