Class: Steep::Interface::MethodType

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

Constant Summary collapse

NONE =
Object.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type_params:, params:, block:, return_type:, location:) ⇒ MethodType

Returns a new instance of MethodType.



602
603
604
605
606
607
608
# File 'lib/steep/interface/method_type.rb', line 602

def initialize(type_params:, params:, block:, return_type:, location:)
  @type_params = type_params
  @params = params
  @block = block
  @return_type = return_type
  @location = location
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



596
597
598
# File 'lib/steep/interface/method_type.rb', line 596

def block
  @block
end

#locationObject (readonly)

Returns the value of attribute location.



598
599
600
# File 'lib/steep/interface/method_type.rb', line 598

def location
  @location
end

#paramsObject (readonly)

Returns the value of attribute params.



595
596
597
# File 'lib/steep/interface/method_type.rb', line 595

def params
  @params
end

#return_typeObject (readonly)

Returns the value of attribute return_type.



597
598
599
# File 'lib/steep/interface/method_type.rb', line 597

def return_type
  @return_type
end

#type_paramsObject (readonly)

Returns the value of attribute type_params.



594
595
596
# File 'lib/steep/interface/method_type.rb', line 594

def type_params
  @type_params
end

Instance Method Details

#+(other) ⇒ Object



686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
# File 'lib/steep/interface/method_type.rb', line 686

def +(other)
  type_params = []
  s1 = Substitution.build(self.type_params)
  type_params.push(*s1.dictionary.values.map(&:name))
  s2 = Substitution.build(other.type_params)
  type_params.push(*s2.dictionary.values.map(&:name))

  block = case
          when self.block && other.block
            self.block.subst(s1) + other.block.subst(s2)
          when self.block
            self.block.to_optional.subst(s1)
          when other.block
            other.block.to_optional.subst(s2)
          end

  self.class.new(
    type_params: type_params,
    params: params.subst(s1) | other.params.subst(s2),
    block: block,
    return_type: AST::Types::Union.build(
      types: [return_type.subst(s1),other.return_type.subst(s2)]
    ),
    location: nil
  )
end

#==(other) ⇒ Object



610
611
612
613
614
615
616
617
# File 'lib/steep/interface/method_type.rb', line 610

def ==(other)
  other.is_a?(self.class) &&
    other.type_params == type_params &&
    other.params == params &&
    other.block == block &&
    other.return_type == return_type &&
    (!other.location || !location || other.location == location)
end

#each_type(&block) ⇒ Object



635
636
637
638
639
640
641
642
643
644
645
646
# File 'lib/steep/interface/method_type.rb', line 635

def each_type(&block)
  if block_given?
    params.each_type(&block)
    self.block&.tap do
      self.block.type.params.each_type(&block)
      yield(self.block.type.return_type)
    end
    yield(return_type)
  else
    enum_for :each_type
  end
end

#free_variablesObject



619
620
621
# File 'lib/steep/interface/method_type.rb', line 619

def free_variables
  (params.free_variables + (block&.free_variables || Set.new) + return_type.free_variables) - Set.new(type_params)
end

#instantiate(s) ⇒ Object



648
649
650
651
652
653
654
655
656
# File 'lib/steep/interface/method_type.rb', line 648

def instantiate(s)
  self.class.new(
    type_params: [],
    params: params.subst(s),
    block: block&.subst(s),
    return_type: return_type.subst(s),
    location: location,
    )
end

#map_type(&block) ⇒ Object



676
677
678
679
680
681
682
683
684
# File 'lib/steep/interface/method_type.rb', line 676

def map_type(&block)
  self.class.new(
    type_params: type_params,
    params: params.map_type(&block),
    block: self.block&.yield_self {|blk| blk.map_type(&block) },
    return_type: yield(return_type),
    location: location
  )
end

#subst(s) ⇒ Object



623
624
625
626
627
628
629
630
631
632
633
# File 'lib/steep/interface/method_type.rb', line 623

def subst(s)
  s_ = s.except(type_params)

  self.class.new(
    type_params: type_params,
    params: params.subst(s_),
    block: block&.subst(s_),
    return_type: return_type.subst(s_),
    location: location
  )
end

#to_sObject



668
669
670
671
672
673
674
# File 'lib/steep/interface/method_type.rb', line 668

def to_s
  type_params = !self.type_params.empty? ? "[#{self.type_params.map{|x| "#{x}" }.join(", ")}] " : ""
  params = self.params.to_s
  block = self.block ? " #{self.block}" : ""

  "#{type_params}#{params}#{block} -> #{return_type}"
end

#with(type_params: NONE, params: NONE, block: NONE, return_type: NONE, location: NONE) ⇒ Object



658
659
660
661
662
663
664
665
666
# File 'lib/steep/interface/method_type.rb', line 658

def with(type_params: NONE, params: NONE, block: NONE, return_type: NONE, location: NONE)
  self.class.new(
    type_params: type_params.equal?(NONE) ? self.type_params : type_params,
    params: params.equal?(NONE) ? self.params : params,
    block: block.equal?(NONE) ? self.block : block,
    return_type: return_type.equal?(NONE) ? self.return_type : return_type,
    location: location.equal?(NONE) ? self.location : location
  )
end