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.



1602
1603
1604
1605
1606
1607
1608
1609
1610
# File 'lib/milk_tea/core/types.rb', line 1602

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.



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

def element_types
  @element_types
end

#field_namesObject (readonly)

Returns the value of attribute field_names.



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

def field_names
  @field_names
end

#hashObject (readonly)

Returns the value of attribute hash.



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

def hash
  @hash
end

Instance Method Details

#childrenObject



1637
1638
1639
# File 'lib/milk_tea/core/types.rb', line 1637

def children
  element_types
end

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

Returns:

  • (Boolean)


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

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

#field(name) ⇒ Object



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

def field(name)
  @fields[name]
end

#fieldsObject



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

def fields
  @fields
end

#to_sObject



1628
1629
1630
1631
1632
1633
1634
1635
# File 'lib/milk_tea/core/types.rb', line 1628

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