Class: MilkTea::Types::Tuple

Inherits:
Base
  • Object
show all
Defined in:
lib/milk_tea/core/types/types.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#accept, #bitwise?, #boolean?, #field_c_name, #float?, #integer?, #numeric?, #sendable?, #void?

Constructor Details

#initialize(element_types, field_names: nil) ⇒ Tuple

Returns a new instance of Tuple.



1566
1567
1568
1569
1570
1571
1572
1573
1574
# File 'lib/milk_tea/core/types/types.rb', line 1566

def initialize(element_types, field_names: nil)
  @element_types = element_types.freeze
  @field_names = (field_names || element_types.each_with_index.map { |_, i| "_#{i}" }).freeze
  @fields = @field_names.each_with_index.each_with_object({}) do |(name, index), h|
    h[name] = element_types[index]
  end.freeze
  @hash = [self.class, @element_types, @field_names].hash
  freeze
end

Instance Attribute Details

#element_typesObject (readonly)

Returns the value of attribute element_types.



1564
1565
1566
# File 'lib/milk_tea/core/types/types.rb', line 1564

def element_types
  @element_types
end

#field_namesObject (readonly)

Returns the value of attribute field_names.



1564
1565
1566
# File 'lib/milk_tea/core/types/types.rb', line 1564

def field_names
  @field_names
end

#hashObject (readonly)

Returns the value of attribute hash.



1582
1583
1584
# File 'lib/milk_tea/core/types/types.rb', line 1582

def hash
  @hash
end

Instance Method Details

#childrenObject



1601
1602
1603
# File 'lib/milk_tea/core/types/types.rb', line 1601

def children
  element_types
end

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

Returns:

  • (Boolean)


1576
1577
1578
# File 'lib/milk_tea/core/types/types.rb', line 1576

def eql?(other)
  other.is_a?(Tuple) && other.element_types == element_types && other.field_names == field_names
end

#field(name) ⇒ Object



1588
1589
1590
# File 'lib/milk_tea/core/types/types.rb', line 1588

def field(name)
  @fields[name]
end

#fieldsObject



1584
1585
1586
# File 'lib/milk_tea/core/types/types.rb', line 1584

def fields
  @fields
end

#to_sObject



1592
1593
1594
1595
1596
1597
1598
1599
# File 'lib/milk_tea/core/types/types.rb', line 1592

def to_s
  if field_names == element_types.each_with_index.map { |_, i| "_#{i}" }
    "(#{element_types.map(&:to_s).join(', ')})"
  else
    fields_parts = field_names.each_with_index.map { |n, i| "#{n}: #{element_types[i]}" }
    "(#{fields_parts.join(', ')})"
  end
end