Module: Alf::Types

Defined in:
lib/alf/types.rb,
lib/alf/types/keys.rb,
lib/alf/types/size.rb,
lib/alf/types/heading.rb,
lib/alf/types/boolean.rb,
lib/alf/types/selector.rb,
lib/alf/types/renaming.rb,
lib/alf/types/ordering.rb,
lib/alf/types/attr_name.rb,
lib/alf/types/attr_list.rb,
lib/alf/types/selection.rb,
lib/alf/types/type_check.rb,
lib/alf/types/summarization.rb,
lib/alf/types/tuple_expression.rb,
lib/alf/types/tuple_computation.rb

Overview

This module defines a namespace for useful datatypes as well as a few typing tools. Defined types are automatically defined as constants on the Alf module itself.

Defined Under Namespace

Classes: AttrList, AttrName, Heading, Keys, Ordering, Renaming, Selection, Selector, Size, Summarization, TupleComputation, TupleExpression, TypeCheck

Constant Summary collapse

Boolean =

Defines the Boolean type which is missing to ruby.

Myrrha::Boolean

Class Method Summary collapse

Class Method Details

.common_super_type(c1, c2) ⇒ Class

Returns the (least) common super type of c1 and c2.

The least common super type is defined as the lowest ancestor shared between c1 and c2.

Examples:

Types.common_super_type(String, String)  # => String
Types.common_super_type(Integer, Float)  # => Numeric
Types.common_super_type(Integer, String) # => Object

22
23
24
25
26
27
28
29
30
31
# File 'lib/alf/types.rb', line 22

def common_super_type(c1, c2)
  return c1 if c1==c2
  return Boolean if ([c1, c2] - [Boolean, TrueClass, FalseClass]).empty?
  if x = (c1 <=> c2)
    x >= 0 ? c1 : c2
  else
    ancestors = [c1, c2].map{|c| c.ancestors.select{|a| a.is_a?(Class)} }
    (ancestors[0] & ancestors[1]).first
  end
end