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.



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

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

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



989
990
991
# File 'lib/rbs/types.rb', line 989

def block
  @block
end

#locationObject (readonly)

Returns the value of attribute location.



990
991
992
# File 'lib/rbs/types.rb', line 990

def location
  @location
end

#typeObject (readonly)

Returns the value of attribute type.



988
989
990
# File 'lib/rbs/types.rb', line 988

def type
  @type
end

Instance Method Details

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



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

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

#each_type(&block) ⇒ Object



1040
1041
1042
1043
1044
1045
1046
1047
# File 'lib/rbs/types.rb', line 1040

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



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

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

#hashObject



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

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

#map_type_name(&block) ⇒ Object



1049
1050
1051
1052
1053
1054
1055
# File 'lib/rbs/types.rb', line 1049

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



1023
1024
1025
# File 'lib/rbs/types.rb', line 1023

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

#to_json(*a) ⇒ Object



1014
1015
1016
1017
1018
1019
1020
1021
# File 'lib/rbs/types.rb', line 1014

def to_json(*a)
  {
    class: :proc,
    type: type,
    block: block,
    location: location
  }.to_json(*a)
end

#to_s(level = 0) ⇒ Object



1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
# File 'lib/rbs/types.rb', line 1027

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