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.



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

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

Instance Attribute Details

#locationObject (readonly)

Returns the value of attribute location.



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

def location
  @location
end

#typesObject (readonly)

Returns the value of attribute types.



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

def types
  @types
end

Instance Method Details

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



527
528
529
# File 'lib/rbs/types.rb', line 527

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

#each_type(&block) ⇒ Object



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

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

#free_variables(set = Set.new) ⇒ Object



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

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

#hashObject



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

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

#map_type(&block) ⇒ Object



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

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

#map_type_name(&block) ⇒ Object



578
579
580
581
582
583
# File 'lib/rbs/types.rb', line 578

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

#sub(s) ⇒ Object



549
550
551
552
# File 'lib/rbs/types.rb', line 549

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

#to_json(*a) ⇒ Object



545
546
547
# File 'lib/rbs/types.rb', line 545

def to_json(*a)
  { class: :union, types: types, location: location }.to_json(*a)
end

#to_s(level = 0) ⇒ Object



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

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