Class: Rigor::Type::Constant
- Inherits:
-
Object
- Object
- Rigor::Type::Constant
- Defined in:
- lib/rigor/type/constant.rb
Overview
A literal carrier under ADR-3 OQ1 Option C (Hybrid). Wraps a Ruby literal value of one of the supported immutable-ish classes. Compound literal shapes (Tuple, HashShape, Record) get dedicated classes in later slices; Range is carried only when both static endpoints are known enough for tuple slicing.
See docs/adr/4-type-inference-engine.md for the tentative answer to the open question and docs/type-specification/rigor-extensions.md for the refinement neighbourhood this carrier lives in.
Constant Summary collapse
- SCALAR_CLASSES =
[ Integer, Float, String, Symbol, Range, Rational, Complex, TrueClass, FalseClass, NilClass ].freeze
- RBS_LITERAL_CLASSES =
{ TrueClass => "true", FalseClass => "false", NilClass => "nil" }.freeze
Instance Attribute Summary collapse
-
#value ⇒ Object
readonly
Returns the value of attribute value.
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(value) ⇒ Constant
constructor
A new instance of Constant.
- #inspect ⇒ Object
- #top ⇒ Object
Constructor Details
#initialize(value) ⇒ Constant
Returns a new instance of Constant.
38 39 40 41 42 43 44 45 |
# File 'lib/rigor/type/constant.rb', line 38 def initialize(value) unless SCALAR_CLASSES.any? { |klass| value.is_a?(klass) } raise ArgumentError, "Rigor::Type::Constant only carries scalar literals; got #{value.class}" end @value = value.is_a?(String) ? value.dup.freeze : value freeze end |
Instance Attribute Details
#value ⇒ Object (readonly)
Returns the value of attribute value.
36 37 38 |
# File 'lib/rigor/type/constant.rb', line 36 def value @value end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
76 77 78 |
# File 'lib/rigor/type/constant.rb', line 76 def ==(other) other.is_a?(Constant) && value.class == other.value.class && value == other.value end |
#accepts(other, mode: :gradual) ⇒ Object
72 73 74 |
# File 'lib/rigor/type/constant.rb', line 72 def accepts(other, mode: :gradual) Inference::Acceptance.accepts(self, other, mode: mode) end |
#describe(_verbosity = :short) ⇒ Object
47 48 49 |
# File 'lib/rigor/type/constant.rb', line 47 def describe(_verbosity = :short) value.inspect end |
#erase_to_rbs ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/rigor/type/constant.rb', line 51 def erase_to_rbs case value when true then "true" when false then "false" when nil then "nil" else value.class.name end end |
#hash ⇒ Object
81 82 83 |
# File 'lib/rigor/type/constant.rb', line 81 def hash [Constant, value.class, value].hash end |
#inspect ⇒ Object
85 86 87 |
# File 'lib/rigor/type/constant.rb', line 85 def inspect "#<Rigor::Type::Constant #{describe(:short)}>" end |