Class: MilkTea::Types::GenericVariantDefinition
- Defined in:
- lib/milk_tea/core/types.rb
Instance Attribute Summary collapse
-
#hash ⇒ Object
readonly
Returns the value of attribute hash.
-
#module_name ⇒ Object
readonly
Returns the value of attribute module_name.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#type_param_constraints ⇒ Object
readonly
Returns the value of attribute type_param_constraints.
-
#type_params ⇒ Object
readonly
Returns the value of attribute type_params.
Instance Method Summary collapse
- #arms ⇒ Object
- #children ⇒ Object
- #define_arms(arms_hash) ⇒ Object
- #define_type_param_constraints(type_param_constraints) ⇒ Object
- #eql?(other) ⇒ Boolean (also: #==)
-
#initialize(name, type_params, module_name: nil) ⇒ GenericVariantDefinition
constructor
A new instance of GenericVariantDefinition.
- #instantiate(arguments) ⇒ Object
- #to_s ⇒ Object
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.
1077 1078 1079 1080 1081 1082 1083 1084 1085 |
# File 'lib/milk_tea/core/types.rb', line 1077 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
#hash ⇒ Object (readonly)
Returns the value of attribute hash.
1110 1111 1112 |
# File 'lib/milk_tea/core/types.rb', line 1110 def hash @hash end |
#module_name ⇒ Object (readonly)
Returns the value of attribute module_name.
1075 1076 1077 |
# File 'lib/milk_tea/core/types.rb', line 1075 def module_name @module_name end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
1075 1076 1077 |
# File 'lib/milk_tea/core/types.rb', line 1075 def name @name end |
#type_param_constraints ⇒ Object (readonly)
Returns the value of attribute type_param_constraints.
1075 1076 1077 |
# File 'lib/milk_tea/core/types.rb', line 1075 def type_param_constraints @type_param_constraints end |
#type_params ⇒ Object (readonly)
Returns the value of attribute type_params.
1075 1076 1077 |
# File 'lib/milk_tea/core/types.rb', line 1075 def type_params @type_params end |
Instance Method Details
#arms ⇒ Object
1092 1093 1094 |
# File 'lib/milk_tea/core/types.rb', line 1092 def arms @arms end |
#children ⇒ Object
1132 1133 1134 |
# File 'lib/milk_tea/core/types.rb', line 1132 def children arms.values.flat_map(&:values) end |
#define_arms(arms_hash) ⇒ Object
1087 1088 1089 1090 |
# File 'lib/milk_tea/core/types.rb', line 1087 def define_arms(arms_hash) @arms = arms_hash.freeze self end |
#define_type_param_constraints(type_param_constraints) ⇒ Object
1096 1097 1098 1099 |
# File 'lib/milk_tea/core/types.rb', line 1096 def define_type_param_constraints(type_param_constraints) @type_param_constraints = type_param_constraints.freeze self end |
#eql?(other) ⇒ Boolean Also known as: ==
1101 1102 1103 1104 1105 1106 |
# File 'lib/milk_tea/core/types.rb', line 1101 def eql?(other) other.class == self.class && other.name == name && other.type_params == type_params && other.module_name == module_name end |
#instantiate(arguments) ⇒ Object
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 |
# File 'lib/milk_tea/core/types.rb', line 1112 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_s ⇒ Object
1128 1129 1130 |
# File 'lib/milk_tea/core/types.rb', line 1128 def to_s module_name ? "#{module_name}.#{name}" : name end |