Class: Steep::Interface::Block

Inherits:
Object
  • Object
show all
Defined in:
lib/steep/interface/block.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, optional:, self_type:) ⇒ Block

Returns a new instance of Block.



8
9
10
11
12
# File 'lib/steep/interface/block.rb', line 8

def initialize(type:, optional:, self_type:)
  @type = type
  @optional = optional
  @self_type = self_type
end

Instance Attribute Details

#optionalObject (readonly)

Returns the value of attribute optional.



5
6
7
# File 'lib/steep/interface/block.rb', line 5

def optional
  @optional
end

#self_typeObject (readonly)

Returns the value of attribute self_type.



6
7
8
# File 'lib/steep/interface/block.rb', line 6

def self_type
  @self_type
end

#typeObject (readonly)

Returns the value of attribute type.



4
5
6
# File 'lib/steep/interface/block.rb', line 4

def type
  @type
end

Instance Method Details

#+(other) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/steep/interface/block.rb', line 82

def +(other)
  optional = self.optional? || other.optional?
  type = Function.new(
    params: self.type.params && other.type.params ? self.type.params + other.type.params : nil,
    return_type: AST::Types::Union.build(types: [self.type.return_type, other.type.return_type]),
    location: nil
  )

  self_types = [self.self_type, other.self_type].compact

  self.class.new(
    type: type,
    optional: optional,
    self_type:
      unless self_types.empty?
        AST::Types::Union.build(types: self_types)
      end
  )
end

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



30
31
32
# File 'lib/steep/interface/block.rb', line 30

def ==(other)
  other.is_a?(self.class) && other.type == type && other.optional == optional && other.self_type == self_type
end

#closed?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/steep/interface/block.rb', line 40

def closed?
  type.closed?
end

#free_variablesObject



55
56
57
# File 'lib/steep/interface/block.rb', line 55

def free_variables()
  @fvs ||= type.free_variables + (self_type&.free_variables || Set[])
end

#hashObject



36
37
38
# File 'lib/steep/interface/block.rb', line 36

def hash
  type.hash ^ optional.hash ^ self_type.hash
end

#map_type(&block) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/steep/interface/block.rb', line 64

def map_type(&block)
  self.class.new(
    type: type.map_type(&block),
    self_type: self_type&.map_type(&block),
    optional: optional
  )
end

#optional?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/steep/interface/block.rb', line 14

def optional?
  @optional
end

#required?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/steep/interface/block.rb', line 18

def required?
  !optional?
end

#subst(s) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/steep/interface/block.rb', line 44

def subst(s)
  ty = type.subst(s)
  st = self_type.subst(s) if self_type

  if ty == type && st == self_type
    self
  else
    self.class.new(type: ty, self_type: st, optional: optional)
  end
end

#to_optionalObject



22
23
24
25
26
27
28
# File 'lib/steep/interface/block.rb', line 22

def to_optional
  self.class.new(
    type: type,
    self_type: self_type,
    optional: true
  )
end

#to_proc_typeObject



72
73
74
75
76
77
78
79
80
# File 'lib/steep/interface/block.rb', line 72

def to_proc_type
  proc = AST::Types::Proc.new(type: type, self_type: self_type, block: nil)

  if optional?
    AST::Types::Union.build(types: [proc, AST::Builtin.nil_type])
  else
    proc
  end
end

#to_sObject



59
60
61
62
# File 'lib/steep/interface/block.rb', line 59

def to_s
  self_binding = self_type ? "[self: #{self_type}] " : ""
  "#{optional? ? "?" : ""}{ #{type.params} #{self_binding}-> #{type.return_type} }"
end