Class: RBS::Types::Optional

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, location:) ⇒ Optional

Returns a new instance of Optional.



659
660
661
662
# File 'lib/rbs/types.rb', line 659

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

Instance Attribute Details

#locationObject (readonly)

Returns the value of attribute location.



657
658
659
# File 'lib/rbs/types.rb', line 657

def location
  @location
end

#typeObject (readonly)

Returns the value of attribute type.



656
657
658
# File 'lib/rbs/types.rb', line 656

def type
  @type
end

Instance Method Details

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



664
665
666
# File 'lib/rbs/types.rb', line 664

def ==(other)
  other.is_a?(Optional) && other.type == type
end

#each_typeObject



702
703
704
705
706
707
708
# File 'lib/rbs/types.rb', line 702

def each_type
  if block_given?
    yield type
  else
    enum_for :each_type
  end
end

#free_variables(set = Set.new) ⇒ Object



674
675
676
# File 'lib/rbs/types.rb', line 674

def free_variables(set = Set.new)
  type.free_variables(set)
end

#has_classish_type?Boolean

Returns:

  • (Boolean)


732
733
734
# File 'lib/rbs/types.rb', line 732

def has_classish_type?
  each_type.any? {|type| type.has_classish_type? }
end

#has_self_type?Boolean

Returns:

  • (Boolean)


728
729
730
# File 'lib/rbs/types.rb', line 728

def has_self_type?
  each_type.any? {|type| type.has_self_type? }
end

#hashObject



670
671
672
# File 'lib/rbs/types.rb', line 670

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

#map_type(&block) ⇒ Object



717
718
719
720
721
722
723
724
725
726
# File 'lib/rbs/types.rb', line 717

def map_type(&block)
  if block
    Optional.new(
      type: yield(type),
      location: location
    )
  else
    enum_for :map_type
  end
end

#map_type_name(&block) ⇒ Object



710
711
712
713
714
715
# File 'lib/rbs/types.rb', line 710

def map_type_name(&block)
  Optional.new(
    type: type.map_type_name(&block),
    location: location
  )
end

#sub(s) ⇒ Object



682
683
684
685
686
# File 'lib/rbs/types.rb', line 682

def sub(s)
  return self if s.empty?

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

#to_json(state = nil) ⇒ Object



678
679
680
# File 'lib/rbs/types.rb', line 678

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

#to_s(level = 0) ⇒ Object



688
689
690
691
692
693
694
695
696
697
698
699
700
# File 'lib/rbs/types.rb', line 688

def to_s(level = 0)
  case t = type
  when RBS::Types::Literal
    case t.literal
    when Symbol
      return "#{type.to_s(1)} ?"
    end
  when RBS::Types::Proc
    return "(#{type.to_s(1)})?"
  end

  "#{type.to_s(1)}?"
end

#with_nonreturn_void?Boolean

Returns:

  • (Boolean)


736
737
738
# File 'lib/rbs/types.rb', line 736

def with_nonreturn_void?
  each_type.any? {|type| type.with_nonreturn_void? } # steep:ignore DeprecatedReference
end