Class: Rigor::Type::AcceptsResult
- Inherits:
-
Object
- Object
- Rigor::Type::AcceptsResult
- Defined in:
- lib/rigor/type/accepts_result.rb
Overview
Immutable value object returned by ‘Rigor::Type#accepts(other, mode:)`. Carries the three-valued answer alongside the boundary mode the answer was computed under and an ordered list of textual reasons describing which rules fired.
AcceptsResult is the dual of ‘SubtypeResult` (Slice 5+). Acceptance answers “is `other` passable to `self` at a method-parameter or assignment boundary?”, consulting the gradual-typing rules in docs/type-specification/value-lattice.md when `mode` is `:gradual`, and the strict subset relation when `mode` is `:strict`. Phase 2c ships full `:gradual` semantics; `:strict` is reserved for later slices and currently raises ArgumentError.
Reasons are stored as plain strings for now. Slice 5+ MAY upgrade them to structured records (rule id, supporting facts, dynamic provenance); callers MUST treat the reasons array as opaque except for human-readable logging.
See docs/internal-spec/internal-type-api.md (“Result Value Objects”).
Instance Attribute Summary collapse
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
-
#reasons ⇒ Object
readonly
Returns the value of attribute reasons.
-
#trinary ⇒ Object
readonly
Returns the value of attribute trinary.
Class Method Summary collapse
- .maybe(mode: :gradual, reasons: nil) ⇒ Object
- .no(mode: :gradual, reasons: nil) ⇒ Object
- .yes(mode: :gradual, reasons: nil) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #hash ⇒ Object
-
#initialize(trinary, mode: :gradual, reasons: nil) ⇒ AcceptsResult
constructor
A new instance of AcceptsResult.
- #inspect ⇒ Object
- #maybe? ⇒ Boolean
- #no? ⇒ Boolean
-
#with_reason(reason) ⇒ Object
Returns a new AcceptsResult whose reasons list is ‘self.reasons` with `reason` appended.
- #yes? ⇒ Boolean
Constructor Details
#initialize(trinary, mode: :gradual, reasons: nil) ⇒ AcceptsResult
Returns a new instance of AcceptsResult.
36 37 38 39 40 41 42 43 44 |
# File 'lib/rigor/type/accepts_result.rb', line 36 def initialize(trinary, mode: :gradual, reasons: nil) raise ArgumentError, "trinary must be Rigor::Trinary, got #{trinary.class}" unless trinary.is_a?(Trinary) raise ArgumentError, "mode must be one of #{MODES.inspect}, got #{mode.inspect}" unless MODES.include?(mode) @trinary = trinary @mode = mode @reasons = normalize_reasons(reasons).freeze freeze end |
Instance Attribute Details
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
30 31 32 |
# File 'lib/rigor/type/accepts_result.rb', line 30 def mode @mode end |
#reasons ⇒ Object (readonly)
Returns the value of attribute reasons.
30 31 32 |
# File 'lib/rigor/type/accepts_result.rb', line 30 def reasons @reasons end |
#trinary ⇒ Object (readonly)
Returns the value of attribute trinary.
30 31 32 |
# File 'lib/rigor/type/accepts_result.rb', line 30 def trinary @trinary end |
Class Method Details
.maybe(mode: :gradual, reasons: nil) ⇒ Object
55 56 57 |
# File 'lib/rigor/type/accepts_result.rb', line 55 def maybe(mode: :gradual, reasons: nil) new(Trinary.maybe, mode: mode, reasons: reasons) end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
82 83 84 85 86 87 |
# File 'lib/rigor/type/accepts_result.rb', line 82 def ==(other) other.is_a?(AcceptsResult) && trinary == other.trinary && mode == other.mode && reasons == other.reasons end |
#hash ⇒ Object
90 91 92 |
# File 'lib/rigor/type/accepts_result.rb', line 90 def hash [AcceptsResult, trinary, mode, reasons].hash end |
#inspect ⇒ Object
94 95 96 |
# File 'lib/rigor/type/accepts_result.rb', line 94 def inspect "#<Rigor::Type::AcceptsResult #{trinary.inspect} mode=#{mode}>" end |
#maybe? ⇒ Boolean
68 69 70 |
# File 'lib/rigor/type/accepts_result.rb', line 68 def maybe? trinary.maybe? end |
#no? ⇒ Boolean
64 65 66 |
# File 'lib/rigor/type/accepts_result.rb', line 64 def no? trinary.no? end |
#with_reason(reason) ⇒ Object
Returns a new AcceptsResult whose reasons list is ‘self.reasons` with `reason` appended. Used by combinator-style routing in Inference::Acceptance to thread context through nested acceptance checks without mutating any object.
76 77 78 79 80 |
# File 'lib/rigor/type/accepts_result.rb', line 76 def with_reason(reason) return self if reason.nil? || reason.empty? self.class.new(trinary, mode: mode, reasons: reasons + [reason]) end |
#yes? ⇒ Boolean
60 61 62 |
# File 'lib/rigor/type/accepts_result.rb', line 60 def yes? trinary.yes? end |