Class: Rigor::Type::DataClass

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

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

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.



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rigor/type/data_class.rb', line 30

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

Returns the value of attribute class_name.

Returns:

  • (String, nil)


25
26
27
# File 'lib/rigor/type/data_class.rb', line 25

def class_name
  @class_name
end

#membersArray[Symbol] (readonly)

Returns the value of attribute members.

Returns:

  • (Array[Symbol])


25
26
27
# File 'lib/rigor/type/data_class.rb', line 25

def members
  @members
end

Instance Method Details

#==Boolean

Parameters:

  • other (Object)

Returns:

  • (Boolean)


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

def ==: (untyped other) -> bool

#acceptsAcceptsResult

Parameters:

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

Returns:



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

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

#botTrinary

Returns:



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

def bot: () -> Trinary

#describe(_verbosity = :short) ⇒ String

Parameters:

  • verbosity (Symbol)

Returns:

  • (String)


43
44
45
46
47
# File 'lib/rigor/type/data_class.rb', line 43

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

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

#dynamicTrinary

Returns:



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

def dynamic: () -> Trinary

#erase_to_rbsString

Returns:

  • (String)


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

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

#hashInteger

Returns:

  • (Integer)


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

def hash: () -> Integer

#inspectString

Returns:

  • (String)


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

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

#topTrinary

Returns:



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

def top: () -> Trinary