Class: Carbon::Concrete::Type::Generic
- Inherits:
-
Object
- Object
- Carbon::Concrete::Type::Generic
- Defined in:
- lib/carbon/concrete/type/generic.rb
Overview
This class is frozen upon initialization. This means that any attempt to modify it will result in an error. In most cases, the attributes on this class will also be frozen, as well.
A generic part of a type. This is normally the T
in
Carbon::Pointer<T>
. Generic portions can also have traits that
the generic type must implement; this keeps track of that too.
A complete example, containing traits, would look like this:
Carbon::Pointer<T: Carbon::Sized + Carbon::Numeric>
. The actual
pointer type may not use those traits.
Instance Attribute Summary collapse
-
#implements ⇒ Set<Type>
readonly
The implements of the generic.
-
#location ⇒ Object
readonly
The location of the type.
-
#name ⇒ Type
readonly
The name of the generic.
Instance Method Summary collapse
-
#==(other) ⇒ ::Boolean
(also: #eql?)
Compares this generic instance to another generic instance.
-
#accept(visitor, *params) ⇒ Object
Accepts the current visitor unto itself.
-
#initialize(name, implements, location: nil) ⇒ Generic
constructor
Initialize the generic part of the type.
-
#sub(mapping) ⇒ Type::Generic, self
Returns the correct generic.
-
#to_s ⇒ ::String
A string representation of the generic.
Constructor Details
#initialize(name, implements, location: nil) ⇒ Generic
Initialize the generic part of the type.
58 59 60 61 62 63 |
# File 'lib/carbon/concrete/type/generic.rb', line 58 def initialize(name, implements, location: nil) @location = location @name = name @implements = Set.new(implements) deep_freeze! end |
Instance Attribute Details
#implements ⇒ Set<Type> (readonly)
The implements of the generic. These are the traits that the generic parameter has to implement; this allows the generic code to make assumptions about the generic type (such as behavior or size) that would otherwise be impossible to make.
42 43 44 |
# File 'lib/carbon/concrete/type/generic.rb', line 42 def implements @implements end |
#location ⇒ Object (readonly)
The location of the type. This is used for interfacing with other programs that require a location of some sort.
49 50 51 |
# File 'lib/carbon/concrete/type/generic.rb', line 49 def location @location end |
#name ⇒ Type (readonly)
The name of the generic. This is the generic type variable that is substituted in later for an actual value. This is normally a single character module name, but can be as complicated as needed.
28 29 30 |
# File 'lib/carbon/concrete/type/generic.rb', line 28 def name @name end |
Instance Method Details
#==(other) ⇒ ::Boolean Also known as: eql?
Compares this generic instance to another generic instance. If the other generic instance is this generic instance it returns true; otherwise, if the other value is a generic, and that generic's #to_s is equal to this one's, it returns true; otherwise, it returns false.
99 100 101 102 |
# File 'lib/carbon/concrete/type/generic.rb', line 99 def ==(other) equal?(other) || (other.is_a?(Type::Generic) && other.to_s == to_s) end |
#accept(visitor, *params) ⇒ Object
Accepts the current visitor unto itself.
128 129 130 |
# File 'lib/carbon/concrete/type/generic.rb', line 128 def accept(visitor, *params) visitor.visit(self, *params) end |
#sub(mapping) ⇒ Type::Generic, self
Returns the correct generic. This is a weird function, but essentially, if the #name's Carbon::Concrete::Type#intern is oen of the keys of the mapping, it returns the new generic; otherwise, it returns this object.
82 83 84 |
# File 'lib/carbon/concrete/type/generic.rb', line 82 def sub(mapping) Generic.new(mapping.fetch(@name, @name), @implements) end |
#to_s ⇒ ::String
A string representation of the generic. If there are no implements, this is a direct call to #name's Carbon::Concrete::Type#to_s; otherwise, this includes the name with a joined string of implements.
116 117 118 119 120 121 122 |
# File 'lib/carbon/concrete/type/generic.rb', line 116 def to_s if @implements.any? "#{@name}: #{@implements.map(&:to_s).join(' + ')}".freeze else @name.to_s end end |