Class: Yadriggy::UnionType

Inherits:
Type
  • Object
show all
Defined in:
lib/yadriggy/type.rb

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Type

#!=, #copy, #eql?, error_found!, #exact_type, #has_role?, role, #supertype

Constructor Details

#initialize(*ts) ⇒ UnionType

Returns a new instance of UnionType.

Parameters:

  • ts (Array<Type>)

    the types included in the union type.



187
188
189
# File 'lib/yadriggy/type.rb', line 187

def initialize(*ts)
  @types = ts.flatten.map {|e| UnionType.role(e)&.types || e }.flatten.uniq
end

Instance Attribute Details

#typesArray<Type> (readonly)

Returns the given types.

Returns:

  • (Array<Type>)

    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

Parameters:

  • ts (Array<Type>)

    the types included in the union type.

Returns:



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.

Parameters:

  • t (Type)

    the other type.

Returns:

  • (Boolean)

    true if self is equivalent to t.



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.

Parameters:

  • t (Type)

    a type.

Returns:

  • (Boolean)

    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

#nameString

Obtains the name of this type.

Returns:

  • (String)

    the type name.



228
229
230
231
# File 'lib/yadriggy/type.rb', line 228

def name
  name = '(' << @types.map{|e| e.name }.join('|') << ')'
  name
end