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.



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

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

Instance Attribute Details

#locationObject (readonly)

Returns the value of attribute location.



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

def location
  @location
end

#typesObject (readonly)

Returns the value of attribute types.



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

def types
  @types
end

Instance Method Details

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



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

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

#each_type(&block) ⇒ Object



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

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

#free_variables(set = Set.new) ⇒ Object



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

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

#hashObject



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

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

#map_type(&block) ⇒ Object



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

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



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

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

#sub(s) ⇒ Object



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

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

#to_json(*a) ⇒ Object



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

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

#to_s(level = 0) ⇒ Object



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

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