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.



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

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

Instance Attribute Details

#locationObject (readonly)

Returns the value of attribute location.



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

def location
  @location
end

#typeObject (readonly)

Returns the value of attribute type.



516
517
518
# File 'lib/rbs/types.rb', line 516

def type
  @type
end

Instance Method Details

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



524
525
526
# File 'lib/rbs/types.rb', line 524

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

#each_typeObject



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

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

#free_variables(set = Set.new) ⇒ Object



534
535
536
# File 'lib/rbs/types.rb', line 534

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

#hashObject



530
531
532
# File 'lib/rbs/types.rb', line 530

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

#map_type(&block) ⇒ Object



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

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



566
567
568
569
570
571
# File 'lib/rbs/types.rb', line 566

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

#sub(s) ⇒ Object



542
543
544
# File 'lib/rbs/types.rb', line 542

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

#to_json(state = _ = nil) ⇒ Object



538
539
540
# File 'lib/rbs/types.rb', line 538

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

#to_s(level = 0) ⇒ Object



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

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