Class: Rigor::Type::StructClass

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Methods included from ValueSemantics

included

Methods included from AcceptanceRouter

#accepts

Methods included from PlainLattice

#bot, #dynamic, #top

Constructor Details

#initialize(members, class_name = nil, keyword_init: false) ⇒ StructClass

Returns a new instance of StructClass.

Parameters:

  • members (Array<Symbol>)

    ordered member names.

  • class_name (String, nil) (defaults to: nil)

    the bound class name, or nil for the anonymous ‘Struct.new(…)` result.

  • keyword_init (Boolean) (defaults to: false)

    the ‘keyword_init:` flag.



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_nameObject (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_initObject (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

#membersObject (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_rbsObject



58
59
60
# File 'lib/rigor/type/struct_class.rb', line 58

def erase_to_rbs
  "singleton(#{class_name || 'Struct'})"
end

#inspectObject



70
71
72
# File 'lib/rigor/type/struct_class.rb', line 70

def inspect
  "#<Rigor::Type::StructClass #{describe(:short)}>"
end