Class: T::UnionType

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*types) ⇒ UnionType

Returns a new instance of UnionType.



39
40
41
# File 'lib/emery/type.rb', line 39

def initialize(*types)
  @types = types
end

Instance Attribute Details

#typesObject (readonly)

Returns the value of attribute types.



38
39
40
# File 'lib/emery/type.rb', line 38

def types
  @types
end

Instance Method Details

#==(other) ⇒ Object



51
52
53
# File 'lib/emery/type.rb', line 51

def ==(other)
  T.instance_of?(UnionType, other) and (self.types - other.types).empty?
end

#check(value) ⇒ Object



45
46
47
48
49
50
# File 'lib/emery/type.rb', line 45

def check(value)
  type = types.find {|t| T.instance_of?(t, value) }
  if type == nil
    raise TypeError.new("Value '#{value.inspect.to_s}' type is #{value.class} - any of #{@types.map { |t| t.to_s}.join(', ')} required")
  end
end

#to_sObject



42
43
44
# File 'lib/emery/type.rb', line 42

def to_s
  "Union[#{types.map { |t| t.to_s}.join(', ')}]"
end