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.



1608
1609
1610
1611
1612
1613
1614
1615
1616
# File 'lib/milk_tea/core/types/types.rb', line 1608

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.



1606
1607
1608
# File 'lib/milk_tea/core/types/types.rb', line 1606

def element_types
  @element_types
end

#field_namesObject (readonly)

Returns the value of attribute field_names.



1606
1607
1608
# File 'lib/milk_tea/core/types/types.rb', line 1606

def field_names
  @field_names
end

#hashObject (readonly)

Returns the value of attribute hash.



1624
1625
1626
# File 'lib/milk_tea/core/types/types.rb', line 1624

def hash
  @hash
end

Instance Method Details

#childrenObject



1643
1644
1645
# File 'lib/milk_tea/core/types/types.rb', line 1643

def children
  element_types
end

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

Returns:

  • (Boolean)


1618
1619
1620
# File 'lib/milk_tea/core/types/types.rb', line 1618

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

#field(name) ⇒ Object



1630
1631
1632
# File 'lib/milk_tea/core/types/types.rb', line 1630

def field(name)
  @fields[name]
end

#fieldsObject



1626
1627
1628
# File 'lib/milk_tea/core/types/types.rb', line 1626

def fields
  @fields
end

#to_sObject



1634
1635
1636
1637
1638
1639
1640
1641
# File 'lib/milk_tea/core/types/types.rb', line 1634

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