Class: Rigor::Type::StructClass
- Inherits:
-
Object
- Object
- Rigor::Type::StructClass
- Includes:
- AcceptanceRouter, PlainLattice, ValueSemantics
- Defined in:
- lib/rigor/type/struct_class.rb,
sig/rigor/type.rbs
Overview
The class object produced by Struct.new(:x, :y) (ADR-48 Struct follow-up). The mutable sibling of
DataClass: it models the class (the value bound to Point in Point = Struct.new(:x, :y), or
the anonymous superclass in class Point < Struct.new(:x, :y)), carrying the ordered member-name
list so Point.new(...) can materialise a StructInstance.
keyword_init records the Struct.new(..., keyword_init: true) flag so .new only materialises a
precise instance for the matching call form — a positional .new(1, 2) on a keyword_init: true
struct, or a keyword .new(x: 1) on a positional struct, is a different runtime shape and must
degrade rather than fold a wrong member map.
class_name carries the binding name when known (the named-subclass form) and is nil for the
anonymous result of a bare Struct.new(...) before it is assigned to a constant.
Equality and hashing are structural over the member list, the class name, and the keyword-init flag.
See docs/adr/48-data-struct-value-folding.md § "Struct follow-up".
Instance Attribute Summary collapse
-
#class_name ⇒ String?
readonly
Returns the value of attribute class_name.
-
#keyword_init ⇒ Boolean
readonly
Returns the value of attribute keyword_init.
-
#members ⇒ Array[Symbol]
readonly
Returns the value of attribute members.
Instance Method Summary collapse
- #== ⇒ Boolean
- #accepts ⇒ AcceptsResult
- #bot ⇒ Trinary
- #describe(_verbosity = :short) ⇒ String
- #dynamic ⇒ Trinary
- #erase_to_rbs ⇒ String
- #hash ⇒ Integer
-
#initialize(members, class_name = nil, keyword_init: false) ⇒ StructClass
constructor
A new instance of StructClass.
- #inspect ⇒ String
- #top ⇒ Trinary
Methods included from ValueSemantics
Constructor Details
#initialize(members, class_name = nil, keyword_init: false) ⇒ StructClass
Returns a new instance of StructClass.
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/rigor/type/struct_class.rb', line 33 def initialize(members, class_name = nil, keyword_init: false) unless members.is_a?(Array) && members.all?(Symbol) raise ArgumentError, "members must be an Array of Symbols, got #{members.inspect}" end unless class_name.nil? || (class_name.is_a?(String) && !class_name.empty?) raise ArgumentError, "class_name must be a non-empty String or nil, got #{class_name.inspect}" end @members = members.dup.freeze @class_name = class_name&.freeze @keyword_init = keyword_init ? true : false freeze end |
Instance Attribute Details
#class_name ⇒ String? (readonly)
Returns the value of attribute class_name.
27 28 29 |
# File 'lib/rigor/type/struct_class.rb', line 27 def class_name @class_name end |
#keyword_init ⇒ Boolean (readonly)
Returns the value of attribute keyword_init.
27 28 29 |
# File 'lib/rigor/type/struct_class.rb', line 27 def keyword_init @keyword_init end |
#members ⇒ Array[Symbol] (readonly)
Returns the value of attribute members.
27 28 29 |
# File 'lib/rigor/type/struct_class.rb', line 27 def members @members end |
Instance Method Details
#== ⇒ Boolean
291 |
# File 'sig/rigor/type.rbs', line 291
def ==: (untyped other) -> bool
|
#accepts ⇒ AcceptsResult
290 |
# File 'sig/rigor/type.rbs', line 290
def accepts: (Type::t other, ?mode: accepts_mode) -> AcceptsResult
|
#describe(_verbosity = :short) ⇒ String
47 48 49 50 51 |
# File 'lib/rigor/type/struct_class.rb', line 47 def describe(_verbosity = :short) return "singleton(#{class_name})" if class_name "Struct.new(#{members.map(&:inspect).join(', ')})" end |
#erase_to_rbs ⇒ String
53 54 55 |
# File 'lib/rigor/type/struct_class.rb', line 53 def erase_to_rbs "singleton(#{class_name || 'Struct'})" end |
#hash ⇒ Integer
292 |
# File 'sig/rigor/type.rbs', line 292
def hash: () -> Integer
|
#inspect ⇒ String
65 66 67 |
# File 'lib/rigor/type/struct_class.rb', line 65 def inspect "#<Rigor::Type::StructClass #{describe(:short)}>" end |