Class: MilkTea::Types::Variant
- Inherits:
-
Base
- Object
- Base
- MilkTea::Types::Variant
show all
- Defined in:
- lib/milk_tea/core/types.rb
Overview
A user-defined tagged union (discriminated union). Each arm may carry zero
or more named payload fields. Arms with no fields carry only the discriminant.
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, module_name: nil) ⇒ Variant
Returns a new instance of Variant.
1035
1036
1037
1038
1039
1040
1041
|
# File 'lib/milk_tea/core/types.rb', line 1035
def initialize(name, module_name: nil)
@name = name
@module_name = module_name
@arms = {} @arm_names = []
@hash = [self.class, name, module_name].hash
end
|
Instance Attribute Details
#hash ⇒ Object
Returns the value of attribute hash.
1068
1069
1070
|
# File 'lib/milk_tea/core/types.rb', line 1068
def hash
@hash
end
|
#module_name ⇒ Object
Returns the value of attribute module_name.
1033
1034
1035
|
# File 'lib/milk_tea/core/types.rb', line 1033
def module_name
@module_name
end
|
#name ⇒ Object
Returns the value of attribute name.
1033
1034
1035
|
# File 'lib/milk_tea/core/types.rb', line 1033
def name
@name
end
|
Instance Method Details
#arm(name) ⇒ Object
1049
1050
1051
|
# File 'lib/milk_tea/core/types.rb', line 1049
def arm(name)
@arms[name]
end
|
#arm_names ⇒ Object
1053
1054
1055
|
# File 'lib/milk_tea/core/types.rb', line 1053
def arm_names
@arm_names
end
|
#children ⇒ Object
1074
1075
1076
|
# File 'lib/milk_tea/core/types.rb', line 1074
def children
arm_names.flat_map { |a| arm(a).values }
end
|
#define_arms(arms_hash) ⇒ Object
1043
1044
1045
1046
1047
|
# File 'lib/milk_tea/core/types.rb', line 1043
def define_arms(arms_hash)
@arms = arms_hash.freeze
@arm_names = arms_hash.keys.freeze
self
end
|
#eql?(other) ⇒ Boolean
Also known as:
==
1062
1063
1064
|
# File 'lib/milk_tea/core/types.rb', line 1062
def eql?(other)
other.class == self.class && other.name == name && other.module_name == module_name
end
|
#has_payload?(arm_name) ⇒ Boolean
1057
1058
1059
1060
|
# File 'lib/milk_tea/core/types.rb', line 1057
def has_payload?(arm_name)
fields = @arms[arm_name]
fields && !fields.empty?
end
|
#to_s ⇒ Object
1070
1071
1072
|
# File 'lib/milk_tea/core/types.rb', line 1070
def to_s
module_name ? "#{module_name}.#{name}" : name
end
|