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.



521
522
523
524
# File 'lib/rbs/types.rb', line 521

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

Instance Attribute Details

#locationObject (readonly)

Returns the value of attribute location.



519
520
521
# File 'lib/rbs/types.rb', line 519

def location
  @location
end

#typeObject (readonly)

Returns the value of attribute type.



518
519
520
# File 'lib/rbs/types.rb', line 518

def type
  @type
end

Instance Method Details

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



526
527
528
# File 'lib/rbs/types.rb', line 526

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

#each_typeObject



560
561
562
563
564
565
566
# File 'lib/rbs/types.rb', line 560

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

#free_variables(set = Set.new) ⇒ Object



536
537
538
# File 'lib/rbs/types.rb', line 536

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

#hashObject



532
533
534
# File 'lib/rbs/types.rb', line 532

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

#map_type(&block) ⇒ Object



575
576
577
578
579
580
581
582
583
584
# File 'lib/rbs/types.rb', line 575

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



568
569
570
571
572
573
# File 'lib/rbs/types.rb', line 568

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

#sub(s) ⇒ Object



544
545
546
# File 'lib/rbs/types.rb', line 544

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

#to_json(state = _ = nil) ⇒ Object



540
541
542
# File 'lib/rbs/types.rb', line 540

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

#to_s(level = 0) ⇒ Object



548
549
550
551
552
553
554
555
556
557
558
# File 'lib/rbs/types.rb', line 548

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

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