Class: Rigor::Type::Union
- Inherits:
-
Object
- Object
- Rigor::Type::Union
- Defined in:
- lib/rigor/type/union.rb
Overview
A normalized non-empty union of two or more distinct types. Unions are constructed exclusively through Rigor::Type::Combinator.union, which flattens nested unions, deduplicates structurally-equal members, and collapses single-member or empty results to the appropriate scalar type. Direct calls to .new are an internal contract: callers MUST pass an already-normalized members array.
See docs/type-specification/normalization.md.
Instance Attribute Summary collapse
-
#members ⇒ Object
readonly
Returns the value of attribute members.
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #accepts(other, mode: :gradual) ⇒ Object
- #bot ⇒ Object
- #describe(verbosity = :short) ⇒ Object
- #dynamic ⇒ Object
- #erase_to_rbs ⇒ Object
- #hash ⇒ Object
-
#initialize(members) ⇒ Union
constructor
A new instance of Union.
- #inspect ⇒ Object
- #top ⇒ Object
Constructor Details
#initialize(members) ⇒ Union
Returns a new instance of Union.
18 19 20 21 22 23 24 25 |
# File 'lib/rigor/type/union.rb', line 18 def initialize(members) unless members.is_a?(Array) && members.size >= 2 raise ArgumentError, "Union requires at least two members; use Combinator.union for normalization" end @members = members.freeze freeze end |
Instance Attribute Details
#members ⇒ Object (readonly)
Returns the value of attribute members.
16 17 18 |
# File 'lib/rigor/type/union.rb', line 16 def members @members end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
51 52 53 |
# File 'lib/rigor/type/union.rb', line 51 def ==(other) other.is_a?(Union) && members == other.members end |
#accepts(other, mode: :gradual) ⇒ Object
47 48 49 |
# File 'lib/rigor/type/union.rb', line 47 def accepts(other, mode: :gradual) Inference::Acceptance.accepts(self, other, mode: mode) end |
#describe(verbosity = :short) ⇒ Object
27 28 29 |
# File 'lib/rigor/type/union.rb', line 27 def describe(verbosity = :short) members.map { |m| m.describe(verbosity) }.join(" | ") end |
#dynamic ⇒ Object
43 44 45 |
# File 'lib/rigor/type/union.rb', line 43 def dynamic members.any? { |m| m.respond_to?(:dynamic) && m.dynamic.yes? } ? Trinary.maybe : Trinary.no end |
#erase_to_rbs ⇒ Object
31 32 33 |
# File 'lib/rigor/type/union.rb', line 31 def erase_to_rbs members.map(&:erase_to_rbs).join(" | ") end |
#hash ⇒ Object
56 57 58 |
# File 'lib/rigor/type/union.rb', line 56 def hash [Union, members].hash end |
#inspect ⇒ Object
60 61 62 |
# File 'lib/rigor/type/union.rb', line 60 def inspect "#<Rigor::Type::Union #{describe(:short)}>" end |