Class: RBS::Types::Proc

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(location:, type:, block:) ⇒ Proc

Returns a new instance of Proc.



997
998
999
1000
1001
# File 'lib/rbs/types.rb', line 997

def initialize(location:, type:, block:)
  @type = type
  @block = block
  @location = location
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



994
995
996
# File 'lib/rbs/types.rb', line 994

def block
  @block
end

#locationObject (readonly)

Returns the value of attribute location.



995
996
997
# File 'lib/rbs/types.rb', line 995

def location
  @location
end

#typeObject (readonly)

Returns the value of attribute type.



993
994
995
# File 'lib/rbs/types.rb', line 993

def type
  @type
end

Instance Method Details

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



1003
1004
1005
# File 'lib/rbs/types.rb', line 1003

def ==(other)
  other.is_a?(Proc) && other.type == type && other.block == block
end

#each_type(&block) ⇒ Object



1045
1046
1047
1048
1049
1050
1051
1052
# File 'lib/rbs/types.rb', line 1045

def each_type(&block)
  if block
    type.each_type(&block)
    self.block&.type&.each_type(&block)
  else
    enum_for :each_type
  end
end

#free_variables(set = ) ⇒ Object



1013
1014
1015
1016
1017
# File 'lib/rbs/types.rb', line 1013

def free_variables(set = Set[])
  type.free_variables(set)
  block&.type&.free_variables(set)
  set
end

#hashObject



1009
1010
1011
# File 'lib/rbs/types.rb', line 1009

def hash
  self.class.hash ^ type.hash ^ block.hash
end

#map_type_name(&block) ⇒ Object



1054
1055
1056
1057
1058
1059
1060
# File 'lib/rbs/types.rb', line 1054

def map_type_name(&block)
  Proc.new(
    type: type.map_type_name(&block),
    block: self.block&.map_type {|type| type.map_type_name(&block) },
    location: location
  )
end

#sub(s) ⇒ Object



1028
1029
1030
# File 'lib/rbs/types.rb', line 1028

def sub(s)
  self.class.new(type: type.sub(s), block: block&.sub(s), location: location)
end

#to_json(state = _ = nil) ⇒ Object



1019
1020
1021
1022
1023
1024
1025
1026
# File 'lib/rbs/types.rb', line 1019

def to_json(state = _ = nil)
  {
    class: :proc,
    type: type,
    block: block,
    location: location
  }.to_json(state)
end

#to_s(level = 0) ⇒ Object



1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
# File 'lib/rbs/types.rb', line 1032

def to_s(level = 0)
  case
  when b = block
    if b.required
      "^(#{type.param_to_s}) { (#{b.type.param_to_s}) -> #{b.type.return_to_s} } -> #{type.return_to_s}"
    else
      "^(#{type.param_to_s}) ?{ (#{b.type.param_to_s}) -> #{b.type.return_to_s} } -> #{type.return_to_s}"
    end
  else
    "^(#{type.param_to_s}) -> #{type.return_to_s}"
  end
end