Class: FunctionType

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(receiver_type, formal_types, return_type) ⇒ FunctionType

Returns a new instance of FunctionType.



8
9
10
11
12
13
# File 'lib/function_type.rb', line 8

def initialize(receiver_type, formal_types, return_type)
  raise "nil not allowed" if formal_types.nil? or return_type.nil?
  @receiver_type = receiver_type
  @formal_types = formal_types
  @return_type = return_type
end

Instance Attribute Details

#formal_typesObject

Returns the value of attribute formal_types.



5
6
7
# File 'lib/function_type.rb', line 5

def formal_types
  @formal_types
end

#receiver_typeObject

Returns the value of attribute receiver_type.



4
5
6
# File 'lib/function_type.rb', line 4

def receiver_type
  @receiver_type
end

#return_typeObject

Returns the value of attribute return_type.



6
7
8
# File 'lib/function_type.rb', line 6

def return_type
  @return_type
end

Instance Method Details

#==(other) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/function_type.rb', line 15

def ==(other)
  return nil unless other.class == self.class

  return false unless other.receiver_type == self.receiver_type
  return false unless other.return_type == self.return_type
  return false unless other.formal_types == self.formal_types
  return true
end

#to_sObject



38
39
40
41
42
43
44
# File 'lib/function_type.rb', line 38

def to_s
  formals = formal_types.map do |t|
    t.inspect
  end

  "function(#{receiver_type.inspect}, [#{formals.join ', '}], #{return_type.inspect})"
end

#unify_components(other) ⇒ Object

Raises:

  • (TypeError)


24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/function_type.rb', line 24

def unify_components(other)
  raise TypeError, "Unable to unify: different number of args #{self.inspect} vs #{other.inspect}" unless
    @formal_types.length == other.formal_types.length

  @formal_types.each_with_index do |t, i|
    t.unify other.formal_types[i]
  end

  @receiver_type.unify other.receiver_type
  @return_type.unify other.return_type
#  rescue RuntimeError # print more complete warning message
#    raise "Unable to unify\n#{self}\nwith\n#{other}"
end