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.



410
411
412
413
# File 'lib/rbs/types.rb', line 410

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

Instance Attribute Details

#locationObject (readonly)

Returns the value of attribute location.



408
409
410
# File 'lib/rbs/types.rb', line 408

def location
  @location
end

#typeObject (readonly)

Returns the value of attribute type.



407
408
409
# File 'lib/rbs/types.rb', line 407

def type
  @type
end

Instance Method Details

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



415
416
417
# File 'lib/rbs/types.rb', line 415

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

#each_typeObject



445
446
447
448
449
450
451
# File 'lib/rbs/types.rb', line 445

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

#free_variables(set = Set.new) ⇒ Object



425
426
427
# File 'lib/rbs/types.rb', line 425

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

#hashObject



421
422
423
# File 'lib/rbs/types.rb', line 421

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

#sub(s) ⇒ Object



433
434
435
# File 'lib/rbs/types.rb', line 433

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

#to_json(*a) ⇒ Object



429
430
431
# File 'lib/rbs/types.rb', line 429

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

#to_s(level = 0) ⇒ Object



437
438
439
440
441
442
443
# File 'lib/rbs/types.rb', line 437

def to_s(level = 0)
  if type.is_a?(RBS::Types::Literal) && type.literal.is_a?(Symbol)
    "#{type.to_s(1)} ?"
  else
    "#{type.to_s(1)}?"
  end
end