Class: Rigor::Type::StructClass

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

Instance Method Summary collapse

Methods included from ValueSemantics

included

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.

  • keyword_init: (Boolean) (defaults to: false)


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_nameString? (readonly)

Returns the value of attribute class_name.

Returns:

  • (String, nil)


27
28
29
# File 'lib/rigor/type/struct_class.rb', line 27

def class_name
  @class_name
end

#keyword_initBoolean (readonly)

Returns the value of attribute keyword_init.

Returns:

  • (Boolean)


27
28
29
# File 'lib/rigor/type/struct_class.rb', line 27

def keyword_init
  @keyword_init
end

#membersArray[Symbol] (readonly)

Returns the value of attribute members.

Returns:

  • (Array[Symbol])


27
28
29
# File 'lib/rigor/type/struct_class.rb', line 27

def members
  @members
end

Instance Method Details

#==Boolean

Parameters:

  • other (Object)

Returns:

  • (Boolean)


291
# File 'sig/rigor/type.rbs', line 291

def ==: (untyped other) -> bool

#acceptsAcceptsResult

Parameters:

  • other (Type::t)
  • mode: (accepts_mode)

Returns:



290
# File 'sig/rigor/type.rbs', line 290

def accepts: (Type::t other, ?mode: accepts_mode) -> AcceptsResult

#botTrinary

Returns:



288
# File 'sig/rigor/type.rbs', line 288

def bot: () -> Trinary

#describe(_verbosity = :short) ⇒ String

Parameters:

  • verbosity (Symbol)

Returns:

  • (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

#dynamicTrinary

Returns:



289
# File 'sig/rigor/type.rbs', line 289

def dynamic: () -> Trinary

#erase_to_rbsString

Returns:

  • (String)


53
54
55
# File 'lib/rigor/type/struct_class.rb', line 53

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

#hashInteger

Returns:

  • (Integer)


292
# File 'sig/rigor/type.rbs', line 292

def hash: () -> Integer

#inspectString

Returns:

  • (String)


65
66
67
# File 'lib/rigor/type/struct_class.rb', line 65

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

#topTrinary

Returns:



287
# File 'sig/rigor/type.rbs', line 287

def top: () -> Trinary