Class: Rigor::Type::DataClass

Inherits:
Object
  • Object
show all
Includes:
AcceptanceRouter, PlainLattice, 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

Methods included from PlainLattice

#bot, #dynamic, #top

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.



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

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.



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

def class_name
  @class_name
end

#membersObject (readonly)

Returns the value of attribute members.



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

def members
  @members
end

Instance Method Details

#describe(_verbosity = :short) ⇒ Object



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

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

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

#erase_to_rbsObject



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

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

#inspectObject



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

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