Class: RBS::Types::Union

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs/types.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(types:, location:) ⇒ Union

Returns a new instance of Union.



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

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

Instance Attribute Details

#locationObject (readonly)

Returns the value of attribute location.



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

def location
  @location
end

#typesObject (readonly)

Returns the value of attribute types.



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

def types
  @types
end

Instance Method Details

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



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

def ==(other)
  other.is_a?(Union) && other.types == types
end

#each_type(&block) ⇒ Object



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

def each_type(&block)
  if block
    types.each(&block)
  else
    enum_for :each_type
  end
end

#free_variables(set = Set.new) ⇒ Object



539
540
541
542
543
544
545
# File 'lib/rbs/types.rb', line 539

def free_variables(set = Set.new)
  set.tap do
    types.each do |type|
      type.free_variables set
    end
  end
end

#hashObject



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

def hash
  self.class.hash ^ types.hash
end

#map_type(&block) ⇒ Object



572
573
574
575
576
577
578
# File 'lib/rbs/types.rb', line 572

def map_type(&block)
  if block
    Union.new(types: types.map(&block), location: location)
  else
    enum_for :map_type
  end
end

#map_type_name(&block) ⇒ Object



580
581
582
583
584
585
# File 'lib/rbs/types.rb', line 580

def map_type_name(&block)
  Union.new(
    types: types.map {|type| type.map_type_name(&block) },
    location: location
  )
end

#sub(s) ⇒ Object



551
552
553
554
# File 'lib/rbs/types.rb', line 551

def sub(s)
  self.class.new(types: types.map {|ty| ty.sub(s) },
                 location: location)
end

#to_json(state = _ = nil) ⇒ Object



547
548
549
# File 'lib/rbs/types.rb', line 547

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

#to_s(level = 0) ⇒ Object



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

def to_s(level = 0)
  if level > 0
    "(#{types.join(" | ")})"
  else
    types.join(" | ")
  end
end