Class: Puppet::Pops::Types::PAnyType
- Inherits:
-
TypedModelObject
- Object
- TypedModelObject
- Puppet::Pops::Types::PAnyType
- Defined in:
- lib/puppet/pops/types/types.rb
Overview
Base type for all types
Direct Known Subclasses
PCallableType, PCatalogEntryType, PCollectionType, PDataType, PDefaultType, PRuntimeType, PScalarType, PStructType, PTupleType, PTypeAliasType, PTypeReferenceType, PTypeWithContainedType, PUndefType, PUnitType, PVariantType
Constant Summary collapse
- DEFAULT =
The default instance of this type. Each type in the type system has this constant declared.
PAnyType.new
Instance Method Summary collapse
- #==(o) ⇒ Object
-
#accept(visitor, guard) ⇒ Object
Accept a visitor that will be sent the message
visit, once withselfas the argument. -
#assignable?(o, guard = nil) ⇒ Boolean
Checks if o is a type that is assignable to this type.
-
#callable?(args, guard = nil) ⇒ Boolean
Returns
trueif this instance is a callable that accepts the given args. -
#callable_args?(callable, guard) ⇒ Boolean
private
Returns
trueif this instance is considered valid as arguments to the givencallable. - #eql?(o) ⇒ Boolean
-
#generalize ⇒ PAnyType
Generalizes value specific types.
- #hash ⇒ Object
-
#instance?(o) ⇒ Boolean
Returns true if the given argument o is an instance of this type.
-
#iterable?(guard = nil) ⇒ Boolean
Returns
trueif an instance of this type is iterable,falseotherwise The method #iterable_type must produce aPIterableTypeinstance when this method returnstrue. -
#iterable_type(guard = nil) ⇒ PIterableType?
private
Returns the
PIterableTypethat this type should be assignable to, ornilif no such type exists. -
#kind_of_callable?(optional = true, guard = nil) ⇒ Boolean
private
Responds
truefor all callables, variants of callables and unless optional is false, all optional callables. -
#normalize(guard = nil) ⇒ PAnyType
Normalizes the type.
-
#simple_name ⇒ String
Strips the class name from all module prefixes, the leading ‘P’ and the ending ‘Type’.
- #to_alias_expanded_s ⇒ Object
- #to_s ⇒ Object
Instance Method Details
#accept(visitor, guard) ⇒ Object
Accept a visitor that will be sent the message visit, once with self as the argument. The visitor will then visit all types that this type contains.
45 46 47 |
# File 'lib/puppet/pops/types/types.rb', line 45 def accept(visitor, guard) visitor.visit(self, guard) end |
#assignable?(o, guard = nil) ⇒ Boolean
Checks if o is a type that is assignable to this type. If o is a Class then it is first converted to a type. If o is a Variant, then it is considered assignable when all its types are assignable
The check for assignable must be guarded against self recursion since self, the given type o, or both, might be a TypeAlias. The initial caller of this method will typically never care about this and hence pass only the first argument, but as soon as a check of a contained type encounters a TypeAlias, then a RecursionGuard instance is created and passed on in all subsequent calls. The recursion is allowed to continue until self recursion has been detected in both self and in the given type. At that point the given type is considered to be assignable to self since all checks up to that point were positive.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/puppet/pops/types/types.rb', line 65 def assignable?(o, guard = nil) case o when Class # Safe to call _assignable directly since a Class never is a Unit or Variant _assignable?(TypeCalculator.singleton.type(o), guard) when PUnitType true when PTypeAliasType # An alias may contain self recursive constructs. if o.self_recursion? guard ||= RecursionGuard.new if guard.add_that(o) == RecursionGuard::SELF_RECURSION_IN_BOTH # Recursion detected both in self and other. This means that other is assignable # to self. This point would not have been reached otherwise true else assignable?(o.resolved_type, guard) end else assignable?(o.resolved_type, guard) end when PVariantType # Assignable if all contained types are assignable o.types.all? { |vt| assignable?(vt, guard) } when PNotUndefType if !(o.type.nil? || o.type.assignable?(PUndefType::DEFAULT)) assignable?(o.type, guard) else _assignable?(o, guard) end else _assignable?(o, guard) end end |
#callable?(args, guard = nil) ⇒ Boolean
Returns true if this instance is a callable that accepts the given args
105 106 107 |
# File 'lib/puppet/pops/types/types.rb', line 105 def callable?(args, guard = nil) args.is_a?(PAnyType) && kind_of_callable? && args.callable_args?(self, guard) end |
#callable_args?(callable, guard) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns true if this instance is considered valid as arguments to the given callable
114 115 116 |
# File 'lib/puppet/pops/types/types.rb', line 114 def callable_args?(callable, guard) false end |
#eql?(o) ⇒ Boolean
185 186 187 |
# File 'lib/puppet/pops/types/types.rb', line 185 def eql?(o) self.class == o.class end |
#generalize ⇒ PAnyType
Generalizes value specific types. Types that are not value specific will return self otherwise the generalized type is returned.
123 124 125 126 |
# File 'lib/puppet/pops/types/types.rb', line 123 def generalize # Applicable to all types that have no variables self end |
#hash ⇒ Object
175 176 177 |
# File 'lib/puppet/pops/types/types.rb', line 175 def hash self.class.hash end |
#instance?(o) ⇒ Boolean
Returns true if the given argument o is an instance of this type
181 182 183 |
# File 'lib/puppet/pops/types/types.rb', line 181 def instance?(o) true end |
#iterable?(guard = nil) ⇒ Boolean
Returns true if an instance of this type is iterable, false otherwise The method #iterable_type must produce a PIterableType instance when this method returns true
155 156 157 |
# File 'lib/puppet/pops/types/types.rb', line 155 def iterable?(guard = nil) false end |
#iterable_type(guard = nil) ⇒ PIterableType?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the PIterableType that this type should be assignable to, or nil if no such type exists. A type that returns a PIterableType must respond true to #iterable?.
171 172 173 |
# File 'lib/puppet/pops/types/types.rb', line 171 def iterable_type(guard = nil) nil end |
#kind_of_callable?(optional = true, guard = nil) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Responds true for all callables, variants of callables and unless optional is false, all optional callables.
145 146 147 |
# File 'lib/puppet/pops/types/types.rb', line 145 def kind_of_callable?(optional = true, guard = nil) false end |
#normalize(guard = nil) ⇒ PAnyType
135 136 137 |
# File 'lib/puppet/pops/types/types.rb', line 135 def normalize(guard = nil) self end |
#simple_name ⇒ String
Strips the class name from all module prefixes, the leading ‘P’ and the ending ‘Type’. I.e. an instance of PVariantType will return ‘Variant’
196 197 198 199 |
# File 'lib/puppet/pops/types/types.rb', line 196 def simple_name n = self.class.name n[n.rindex('::')+3..n.size-5] end |
#to_alias_expanded_s ⇒ Object
201 202 203 |
# File 'lib/puppet/pops/types/types.rb', line 201 def TypeFormatter.new.(self) end |
#to_s ⇒ Object
205 206 207 |
# File 'lib/puppet/pops/types/types.rb', line 205 def to_s TypeFormatter.string(self) end |