Class: Camille::Types::Union

Inherits:
BasicType show all
Defined in:
lib/camille/types/union.rb

Instance Attribute Summary collapse

Attributes inherited from BasicType

#fingerprint

Instance Method Summary collapse

Methods inherited from BasicType

&, #&, #[], [], directly_instantiable?, inherited, instance, #transform, #|, |

Constructor Details

#initialize(left, right) ⇒ Union

Returns a new instance of Union.



6
7
8
9
10
# File 'lib/camille/types/union.rb', line 6

def initialize left, right
  @left = Camille::Type.instance left
  @right = Camille::Type.instance right
  @fingerprint = Digest::MD5.hexdigest "#{self.class.name}#{[@left.fingerprint, @right.fingerprint].sort}"
end

Instance Attribute Details

#leftObject (readonly)

Returns the value of attribute left.



4
5
6
# File 'lib/camille/types/union.rb', line 4

def left
  @left
end

#rightObject (readonly)

Returns the value of attribute right.



4
5
6
# File 'lib/camille/types/union.rb', line 4

def right
  @right
end

Instance Method Details

#check(value) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/camille/types/union.rb', line 12

def check value
  left_result = @left.check value
  if left_result.type_error?
    right_result = @right.check value
    if right_result.type_error?
      Camille::TypeError.new(
        'union.left' => left_result,
        'union.right' => right_result
      )
    else
      Camille::Checked.new(fingerprint, right_result.value)
    end
  else
    Camille::Checked.new(fingerprint, left_result.value)
  end
end

#literalObject



29
30
31
# File 'lib/camille/types/union.rb', line 29

def literal
  "(#{@left.literal} | #{@right.literal})"
end