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.



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

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

Instance Attribute Details

#locationObject (readonly)

Returns the value of attribute location.



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

def location
  @location
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

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



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

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

#each_typeObject



505
506
507
508
509
510
511
# File 'lib/rbs/types.rb', line 505

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

#free_variables(set = Set.new) ⇒ Object



481
482
483
# File 'lib/rbs/types.rb', line 481

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

#hashObject



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

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

#map_type_name(&block) ⇒ Object



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

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

#sub(s) ⇒ Object



489
490
491
# File 'lib/rbs/types.rb', line 489

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

#to_json(*a) ⇒ Object



485
486
487
# File 'lib/rbs/types.rb', line 485

def to_json(*a)
  { class: :optional, type: type, location: location }.to_json(*a)
end

#to_s(level = 0) ⇒ Object



493
494
495
496
497
498
499
500
501
502
503
# File 'lib/rbs/types.rb', line 493

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