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.



985
986
987
988
989
# File 'lib/rbs/types.rb', line 985

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

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



982
983
984
# File 'lib/rbs/types.rb', line 982

def block
  @block
end

#locationObject (readonly)

Returns the value of attribute location.



983
984
985
# File 'lib/rbs/types.rb', line 983

def location
  @location
end

#typeObject (readonly)

Returns the value of attribute type.



981
982
983
# File 'lib/rbs/types.rb', line 981

def type
  @type
end

Instance Method Details

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



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

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

#each_type(&block) ⇒ Object



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

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



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

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

#hashObject



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

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

#map_type_name(&block) ⇒ Object



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

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



1016
1017
1018
# File 'lib/rbs/types.rb', line 1016

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

#to_json(state = _ = nil) ⇒ Object



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

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

#to_s(level = 0) ⇒ Object



1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
# File 'lib/rbs/types.rb', line 1020

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