Module: RDL::Type

Defined in:
lib/rdl/types/bot.rb,
lib/rdl/types/top.rb,
lib/rdl/types/var.rb,
lib/rdl/types/type.rb,
lib/rdl/types/tuple.rb,
lib/rdl/types/union.rb,
lib/rdl/types/method.rb,
lib/rdl/types/string.rb,
lib/rdl/types/vararg.rb,
lib/rdl/types/dynamic.rb,
lib/rdl/types/generic.rb,
lib/rdl/types/nominal.rb,
lib/rdl/types/ast_node.rb,
lib/rdl/types/computed.rb,
lib/rdl/types/non_null.rb,
lib/rdl/types/optional.rb,
lib/rdl/types/bound_arg.rb,
lib/rdl/types/lexer.rex.rb,
lib/rdl/types/singleton.rb,
lib/rdl/types/dots_query.rb,
lib/rdl/types/parser.tab.rb,
lib/rdl/types/structural.rb,
lib/rdl/types/type_query.rb,
lib/rdl/types/wild_query.rb,
lib/rdl/types/finite_hash.rb,
lib/rdl/types/intersection.rb,
lib/rdl/types/annotated_arg.rb,
lib/rdl/types/dependent_arg.rb

Defined Under Namespace

Classes: AnnotatedArgType, AstNode, BotType, BoundArgType, ComputedType, DependentArgType, DotsQuery, DynamicType, FiniteHashType, GenericType, IntersectionType, MethodType, NominalType, NonNullType, OptionalType, Parser, PreciseStringType, SingletonType, StructuralType, TopType, TupleType, Type, TypeError, TypeQuery, UnionType, VarType, VarargType, WildQuery

Class Method Summary collapse

Class Method Details

.expand_product(a) ⇒ Object

+ a +

is an Array<Type> that may contain union types.

returns Array<Array<Type>> containing all possible expansions of the union types. For example, slightly abusing notation:

expand_product [A, B] #=> [[A, B]] expand_product [A or B, C] #=> [[A, C], [B, C]] expand_product [A or B, C or D] #=> [[A, C], [B, C], [A, D], [B, D]]



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/rdl/types/type.rb', line 263

def self.expand_product(a)
  return [[]] if a.empty? # logic below only applies if at least one element
  a.map! { |t| t.canonical }
  counts = a.map { |t| if t.is_a? UnionType then t.types.length - 1 else 0 end }
  res = []
  # now iterate through ever combination of indices
  # using combinations is not quite as memory efficient as inlining that code here,
  # but it's a lot easier to think about combinations separate from this code
  combinations(counts).each { |inds|
    tmp = []
    # set tmp to be a with elts in positions in ind selected from unions
    a.each_with_index { |t, i| if t.is_a? UnionType then tmp << t.types[inds[i]] else tmp << t end }
    res << tmp
  }
  return res
#    return [a]
end