Class: Rigor::Type::StructClass
- Inherits:
-
Object
- Object
- Rigor::Type::StructClass
- Includes:
- AcceptanceRouter, PlainLattice, ValueSemantics
- Defined in:
- lib/rigor/type/struct_class.rb
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 ⇒ Object
readonly
Returns the value of attribute class_name.
-
#keyword_init ⇒ Object
readonly
Returns the value of attribute keyword_init.
-
#members ⇒ Object
readonly
Returns the value of attribute members.
Instance Method Summary collapse
- #describe(_verbosity = :short) ⇒ Object
- #erase_to_rbs ⇒ Object
-
#initialize(members, class_name = nil, keyword_init: false) ⇒ StructClass
constructor
A new instance of StructClass.
- #inspect ⇒ Object
Methods included from ValueSemantics
Methods included from AcceptanceRouter
Methods included from PlainLattice
Constructor Details
#initialize(members, class_name = nil, keyword_init: false) ⇒ StructClass
Returns a new instance of StructClass.
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/rigor/type/struct_class.rb', line 38 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 ⇒ Object (readonly)
Returns the value of attribute class_name.
32 33 34 |
# File 'lib/rigor/type/struct_class.rb', line 32 def class_name @class_name end |
#keyword_init ⇒ Object (readonly)
Returns the value of attribute keyword_init.
32 33 34 |
# File 'lib/rigor/type/struct_class.rb', line 32 def keyword_init @keyword_init end |
#members ⇒ Object (readonly)
Returns the value of attribute members.
32 33 34 |
# File 'lib/rigor/type/struct_class.rb', line 32 def members @members end |
Instance Method Details
#describe(_verbosity = :short) ⇒ Object
52 53 54 55 56 |
# File 'lib/rigor/type/struct_class.rb', line 52 def describe(_verbosity = :short) return "singleton(#{class_name})" if class_name "Struct.new(#{members.map(&:inspect).join(', ')})" end |
#erase_to_rbs ⇒ Object
58 59 60 |
# File 'lib/rigor/type/struct_class.rb', line 58 def erase_to_rbs "singleton(#{class_name || 'Struct'})" end |
#inspect ⇒ Object
70 71 72 |
# File 'lib/rigor/type/struct_class.rb', line 70 def inspect "#<Rigor::Type::StructClass #{describe(:short)}>" end |