Class: Rigor::Type::Singleton

Inherits:
Object
  • Object
show all
Defined in:
lib/rigor/type/singleton.rb

Overview

The singleton type for a Ruby class or module. Inhabitants are the class object itself (e.g. the constant ‘Foo`), not its instances. In RBS this corresponds to `singleton(Foo)`.

Singleton` and `Nominal` share the same `class_name` but are NEVER equal; they describe disjoint values (the class object vs. instances of the class).

See docs/type-specification/rbs-compatible-types.md (singleton(T)).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(class_name) ⇒ Singleton

Returns a new instance of Singleton.

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
# File 'lib/rigor/type/singleton.rb', line 19

def initialize(class_name)
  raise ArgumentError, "class_name must be a String, got #{class_name.class}" unless class_name.is_a?(String)
  raise ArgumentError, "class_name must not be empty" if class_name.empty?

  @class_name = class_name.freeze
  freeze
end

Instance Attribute Details

#class_nameObject (readonly)

Returns the value of attribute class_name.



17
18
19
# File 'lib/rigor/type/singleton.rb', line 17

def class_name
  @class_name
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



51
52
53
# File 'lib/rigor/type/singleton.rb', line 51

def ==(other)
  other.is_a?(Singleton) && class_name == other.class_name
end

#accepts(other, mode: :gradual) ⇒ Object



47
48
49
# File 'lib/rigor/type/singleton.rb', line 47

def accepts(other, mode: :gradual)
  Inference::Acceptance.accepts(self, other, mode: mode)
end

#botObject



39
40
41
# File 'lib/rigor/type/singleton.rb', line 39

def bot
  Trinary.no
end

#describe(_verbosity = :short) ⇒ Object



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

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

#dynamicObject



43
44
45
# File 'lib/rigor/type/singleton.rb', line 43

def dynamic
  Trinary.no
end

#erase_to_rbsObject



31
32
33
# File 'lib/rigor/type/singleton.rb', line 31

def erase_to_rbs
  "singleton(#{class_name})"
end

#hashObject



56
57
58
# File 'lib/rigor/type/singleton.rb', line 56

def hash
  [Singleton, class_name].hash
end

#inspectObject



60
61
62
# File 'lib/rigor/type/singleton.rb', line 60

def inspect
  "#<Rigor::Type::Singleton #{class_name}>"
end

#topObject



35
36
37
# File 'lib/rigor/type/singleton.rb', line 35

def top
  Trinary.no
end