Class: MilkTea::Types::GenericVariantDefinition

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(name, type_params, module_name: nil) ⇒ GenericVariantDefinition

Returns a new instance of GenericVariantDefinition.



1088
1089
1090
1091
1092
1093
1094
1095
1096
# File 'lib/milk_tea/core/types/types.rb', line 1088

def initialize(name, type_params, module_name: nil)
  @name = name
  @type_params = type_params.freeze
  @type_param_constraints = {}.freeze
  @module_name = module_name
  @arms = {}
  @instances = {}
  @hash = [self.class, name, @type_params, module_name].hash
end

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



1121
1122
1123
# File 'lib/milk_tea/core/types/types.rb', line 1121

def hash
  @hash
end

#module_nameObject (readonly)

Returns the value of attribute module_name.



1086
1087
1088
# File 'lib/milk_tea/core/types/types.rb', line 1086

def module_name
  @module_name
end

#nameObject (readonly)

Returns the value of attribute name.



1086
1087
1088
# File 'lib/milk_tea/core/types/types.rb', line 1086

def name
  @name
end

#type_param_constraintsObject (readonly)

Returns the value of attribute type_param_constraints.



1086
1087
1088
# File 'lib/milk_tea/core/types/types.rb', line 1086

def type_param_constraints
  @type_param_constraints
end

#type_paramsObject (readonly)

Returns the value of attribute type_params.



1086
1087
1088
# File 'lib/milk_tea/core/types/types.rb', line 1086

def type_params
  @type_params
end

Instance Method Details

#armsObject



1103
1104
1105
# File 'lib/milk_tea/core/types/types.rb', line 1103

def arms
  @arms
end

#childrenObject



1143
1144
1145
# File 'lib/milk_tea/core/types/types.rb', line 1143

def children
  arms.values.flat_map(&:values)
end

#define_arms(arms_hash) ⇒ Object



1098
1099
1100
1101
# File 'lib/milk_tea/core/types/types.rb', line 1098

def define_arms(arms_hash)
  @arms = arms_hash.freeze
  self
end

#define_type_param_constraints(type_param_constraints) ⇒ Object



1107
1108
1109
1110
# File 'lib/milk_tea/core/types/types.rb', line 1107

def define_type_param_constraints(type_param_constraints)
  @type_param_constraints = type_param_constraints.freeze
  self
end

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

Returns:

  • (Boolean)


1112
1113
1114
1115
1116
1117
# File 'lib/milk_tea/core/types/types.rb', line 1112

def eql?(other)
  other.class == self.class &&
    other.name == name &&
    other.type_params == type_params &&
    other.module_name == module_name
end

#instantiate(arguments) ⇒ Object

Raises:

  • (ArgumentError)


1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
# File 'lib/milk_tea/core/types/types.rb', line 1123

def instantiate(arguments)
  raise ArgumentError, "#{name} expects #{type_params.length} type arguments, got #{arguments.length}" unless arguments.length == type_params.length

  key = arguments.dup.freeze
  return @instances[key] if @instances.key?(key)

  substitutions = type_params.zip(arguments).to_h
  instance = VariantInstance.new(self, arguments)
  @instances[key] = instance
  instance.define_arms(
    @arms.transform_values do |fields|
      fields.transform_values { |type| Types.substitute_type_variables(type, substitutions) }
    end,
  )
end

#to_sObject



1139
1140
1141
# File 'lib/milk_tea/core/types/types.rb', line 1139

def to_s
  module_name ? "#{module_name}.#{name}" : name
end