Class: Steep::Subtyping::Relation

Inherits:
Object
  • Object
show all
Defined in:
lib/steep/subtyping/relation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sub_type:, super_type:) ⇒ Relation

Returns a new instance of Relation.



7
8
9
10
# File 'lib/steep/subtyping/relation.rb', line 7

def initialize(sub_type:, super_type:)
  @sub_type = sub_type
  @super_type = super_type
end

Instance Attribute Details

#sub_typeObject (readonly)

Returns the value of attribute sub_type.



4
5
6
# File 'lib/steep/subtyping/relation.rb', line 4

def sub_type
  @sub_type
end

#super_typeObject (readonly)

Returns the value of attribute super_type.



5
6
7
# File 'lib/steep/subtyping/relation.rb', line 5

def super_type
  @super_type
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



16
17
18
# File 'lib/steep/subtyping/relation.rb', line 16

def ==(other)
  other.is_a?(self.class) && other.sub_type == sub_type && other.super_type == super_type
end

#assert_type(type) ⇒ Object



56
57
58
59
60
# File 'lib/steep/subtyping/relation.rb', line 56

def assert_type(type)
  unless __send__(:"#{type}?")
    raise "#{type}? is expected but: sub_type=#{sub_type.class}, super_type=#{super_type.class}"
  end
end

#block!Object



82
83
84
# File 'lib/steep/subtyping/relation.rb', line 82

def block!
  assert_type(:block)
end

#block?Boolean

Returns:

  • (Boolean)


51
52
53
54
# File 'lib/steep/subtyping/relation.rb', line 51

def block?
  (sub_type.is_a?(Interface::Block) || !sub_type) &&
    (!super_type || super_type.is_a?(Interface::Block))
end

#flipObject



93
94
95
96
97
98
# File 'lib/steep/subtyping/relation.rb', line 93

def flip
  self.class.new(
    sub_type: super_type,
    super_type: sub_type
  )
end

#function!Object



74
75
76
# File 'lib/steep/subtyping/relation.rb', line 74

def function!
  assert_type(:function)
end

#function?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/steep/subtyping/relation.rb', line 43

def function?
  sub_type.is_a?(Interface::Function) && super_type.is_a?(Interface::Function)
end

#hashObject



12
13
14
# File 'lib/steep/subtyping/relation.rb', line 12

def hash
  self.class.hash ^ sub_type.hash ^ super_type.hash
end

#interface!Object



66
67
68
# File 'lib/steep/subtyping/relation.rb', line 66

def interface!
  assert_type(:interface)
end

#interface?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/steep/subtyping/relation.rb', line 34

def interface?
  sub_type.is_a?(Interface::Shape) && super_type.is_a?(Interface::Shape)
end

#mapObject



86
87
88
89
90
91
# File 'lib/steep/subtyping/relation.rb', line 86

def map
  self.class.new(
    sub_type: yield(sub_type),
    super_type: yield(super_type)
  )
end

#method!Object



70
71
72
# File 'lib/steep/subtyping/relation.rb', line 70

def method!
  assert_type(:method)
end

#method?Boolean

Returns:

  • (Boolean)


38
39
40
41
# File 'lib/steep/subtyping/relation.rb', line 38

def method?
  (sub_type.is_a?(Interface::Shape::Entry) || sub_type.is_a?(Interface::MethodType)) &&
    (super_type.is_a?(Interface::Shape::Entry) || super_type.is_a?(Interface::MethodType))
end

#params!Object



78
79
80
# File 'lib/steep/subtyping/relation.rb', line 78

def params!
  assert_type(:params)
end

#params?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/steep/subtyping/relation.rb', line 47

def params?
  sub_type.is_a?(Interface::Function::Params) && super_type.is_a?(Interface::Function::Params)
end

#to_aryObject



26
27
28
# File 'lib/steep/subtyping/relation.rb', line 26

def to_ary
  [sub_type, super_type]
end

#to_sObject



22
23
24
# File 'lib/steep/subtyping/relation.rb', line 22

def to_s
  "#{sub_type} <: #{super_type}"
end

#type!Object



62
63
64
# File 'lib/steep/subtyping/relation.rb', line 62

def type!
  assert_type(:type)
end

#type?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/steep/subtyping/relation.rb', line 30

def type?
  !interface? && !method? && !function? && !params? && !block?
end