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.



453
454
455
456
# File 'lib/rbs/types.rb', line 453

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

Instance Attribute Details

#locationObject (readonly)

Returns the value of attribute location.



451
452
453
# File 'lib/rbs/types.rb', line 451

def location
  @location
end

#typeObject (readonly)

Returns the value of attribute type.



450
451
452
# File 'lib/rbs/types.rb', line 450

def type
  @type
end

Instance Method Details

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



458
459
460
# File 'lib/rbs/types.rb', line 458

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

#each_typeObject



492
493
494
495
496
497
498
# File 'lib/rbs/types.rb', line 492

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

#free_variables(set = Set.new) ⇒ Object



468
469
470
# File 'lib/rbs/types.rb', line 468

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

#hashObject



464
465
466
# File 'lib/rbs/types.rb', line 464

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

#map_type_name(&block) ⇒ Object



500
501
502
503
504
505
# File 'lib/rbs/types.rb', line 500

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

#sub(s) ⇒ Object



476
477
478
# File 'lib/rbs/types.rb', line 476

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

#to_json(state = _ = nil) ⇒ Object



472
473
474
# File 'lib/rbs/types.rb', line 472

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

#to_s(level = 0) ⇒ Object



480
481
482
483
484
485
486
487
488
489
490
# File 'lib/rbs/types.rb', line 480

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