Class: MilkTea::Types::Tuple

Inherits:
Base
  • Object
show all
Defined in:
lib/milk_tea/core/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.



1597
1598
1599
1600
1601
1602
1603
1604
1605
# File 'lib/milk_tea/core/types.rb', line 1597

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.



1595
1596
1597
# File 'lib/milk_tea/core/types.rb', line 1595

def element_types
  @element_types
end

#field_namesObject (readonly)

Returns the value of attribute field_names.



1595
1596
1597
# File 'lib/milk_tea/core/types.rb', line 1595

def field_names
  @field_names
end

#hashObject (readonly)

Returns the value of attribute hash.



1613
1614
1615
# File 'lib/milk_tea/core/types.rb', line 1613

def hash
  @hash
end

Instance Method Details

#childrenObject



1632
1633
1634
# File 'lib/milk_tea/core/types.rb', line 1632

def children
  element_types
end

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

Returns:

  • (Boolean)


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

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

#field(name) ⇒ Object



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

def field(name)
  @fields[name]
end

#fieldsObject



1615
1616
1617
# File 'lib/milk_tea/core/types.rb', line 1615

def fields
  @fields
end

#to_sObject



1623
1624
1625
1626
1627
1628
1629
1630
# File 'lib/milk_tea/core/types.rb', line 1623

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