Class: Rigor::Type::DataClass

Inherits:
Object
  • Object
show all
Includes:
AcceptanceRouter, ValueSemantics
Defined in:
lib/rigor/type/data_class.rb

Overview

The class object produced by ‘Data.define(:x, :y)` (ADR-48). Models the class, not an instance — the value bound to `Point` in `Point = Data.define(:x, :y)` or the anonymous superclass in `class Point < Data.define(:x, :y)`. Parameterised by the ordered member-name list so that `Point.new(…)` can materialise a precise DataInstance.

‘class_name` carries the binding name when known (the named-subclass form, ADR-48 slice 3) and is `nil` for the anonymous result of a bare `Data.define(…)` before it is assigned to a constant. It tags the instances `.new` produces; it does not change the carrier’s folding behaviour.

Equality and hashing are structural over the member list and the class name. Two distinct ‘Data.define(:x)` results are equal *as types* — they describe the same shape; the engine distinguishes the constants they are bound to by the binding, not by the carrier.

See docs/adr/48-data-struct-value-folding.md.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ValueSemantics

included

Methods included from AcceptanceRouter

#accepts

Constructor Details

#initialize(members, class_name = nil) ⇒ DataClass

Returns a new instance of DataClass.

Parameters:

  • members (Array<Symbol>)

    ordered member names.

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

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



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rigor/type/data_class.rb', line 34

def initialize(members, class_name = nil)
  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
  freeze
end

Instance Attribute Details

#class_nameObject (readonly)

Returns the value of attribute class_name.



29
30
31
# File 'lib/rigor/type/data_class.rb', line 29

def class_name
  @class_name
end

#membersObject (readonly)

Returns the value of attribute members.



29
30
31
# File 'lib/rigor/type/data_class.rb', line 29

def members
  @members
end

Instance Method Details

#botObject



61
62
63
# File 'lib/rigor/type/data_class.rb', line 61

def bot
  Trinary.no
end

#describe(_verbosity = :short) ⇒ Object



47
48
49
50
51
# File 'lib/rigor/type/data_class.rb', line 47

def describe(_verbosity = :short)
  return "singleton(#{class_name})" if class_name

  "Data.define(#{members.map(&:inspect).join(', ')})"
end

#dynamicObject



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

def dynamic
  Trinary.no
end

#erase_to_rbsObject



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

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

#inspectObject



75
76
77
# File 'lib/rigor/type/data_class.rb', line 75

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

#topObject



57
58
59
# File 'lib/rigor/type/data_class.rb', line 57

def top
  Trinary.no
end