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
PBinaryType, PCallableType, PCatalogEntryType, PCollectionType, PDefaultType, PMetaType, PObjectTypeExtension, PRuntimeType, PScalarType, PSemVerRangeType, PStructType, PTupleType, PTypeAliasType, PTypeReferenceType, PTypeWithContainedType, PURIType, 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
Class Method Summary collapse
- .create(*args) ⇒ Object
-
.new_function(type) ⇒ Function
This default implementation of of a new_function raises an Argument Error.
- .register_ptype(loader, ir) ⇒ Object
-
.simple_name ⇒ String
Strips the class name from all module prefixes, the leading 'P' and the ending 'Type'.
Instance Method Summary collapse
- #==(o) ⇒ Object
-
#accept(visitor, guard) ⇒ Object
Accept a visitor that will be sent the message `visit`, once with `self` as the argument.
-
#assignable?(o, guard = nil) ⇒ Boolean
Checks if o is a type that is assignable to this type.
-
#callable?(args_type, guard = nil) ⇒ Boolean
Returns `true` if this instance is a callable that accepts the given args_type type.
-
#callable_args?(callable, guard) ⇒ Boolean
private
Returns `true` if this instance is considered valid as arguments to the given `callable`.
-
#callable_with?(args, block = nil) ⇒ Boolean
Returns `true` if this instance is a callable that accepts the given args.
-
#check_self_recursion(originator) ⇒ Object
private
Called from the `PTypeAliasType` when it detects self recursion.
- #create(*args) ⇒ Object
- #eql?(o) ⇒ Boolean
-
#generalize ⇒ PAnyType
Generalizes value specific types.
- #hash ⇒ Object
-
#instance?(o, guard = nil) ⇒ Boolean
Returns true if the given argument o is an instance of this type.
-
#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`.
-
#iterable_type(guard = nil) ⇒ PIterableType?
private
Returns the `PIterableType` that this type should be assignable to, or `nil` if no such type exists.
-
#kind_of_callable?(optional = true, guard = nil) ⇒ Boolean
private
Responds `true` for all callables, variants of callables and unless optional is false, all optional callables.
-
#loader ⇒ Loaders::Loader
Returns the loader that loaded this type.
-
#name ⇒ String
Returns the name of the type, without parameters.
-
#new_function ⇒ Function
Create an instance of this type.
-
#normalize(guard = nil) ⇒ PAnyType
Normalizes the type.
-
#really_instance?(o, guard = nil) ⇒ Integer
private
An object is considered to really be an instance of a type when something other than a TypeAlias or a Variant responds true to a call to #instance?.
-
#resolve(loader) ⇒ PTypeAliasType
private
Called from the TypeParser once it has found a type using the Loader to enable that this type can resolve internal type expressions using a loader.
-
#roundtrip_with_string? ⇒ Boolean
Answers the question if instances of this type can represent themselves as a string that can then be passed to the create method.
- #simple_name ⇒ Object
- #to_alias_expanded_s ⇒ Object
- #to_s ⇒ Object
Methods inherited from TypedModelObject
_pcore_type, create_ptype, register_ptypes
Methods included from PuppetObject
#_pcore_all_contents, #_pcore_contents, #_pcore_init_hash, #_pcore_type
Class Method Details
.create(*args) ⇒ Object
81 82 83 84 |
# File 'lib/puppet/pops/types/types.rb', line 81 def self.create(*args) # NOTE! Important to use self::DEFAULT and not just DEFAULT since the latter yields PAnyType::DEFAULT args.empty? ? self::DEFAULT : new(*args) end |
.new_function(type) ⇒ Function
This default implementation of of a new_function raises an Argument Error. Types for which creating a new instance is supported, should create and return a Puppet Function class by using Puppet:Loaders.create_loaded_function(:new, loader) and return that result.
341 342 343 |
# File 'lib/puppet/pops/types/types.rb', line 341 def self.new_function(type) raise ArgumentError.new("Creation of new instance of type '#{type}' is not supported") end |
.register_ptype(loader, ir) ⇒ Object
77 78 79 |
# File 'lib/puppet/pops/types/types.rb', line 77 def self.register_ptype(loader, ir) @type = Pcore::create_object_type(loader, ir, self, 'Pcore::AnyType', 'Any', EMPTY_HASH) 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'
295 296 297 298 299 300 |
# File 'lib/puppet/pops/types/types.rb', line 295 def self.simple_name @simple_name ||= ( n = name n[n.rindex(DOUBLE_COLON)+3..n.size-5].freeze ) end |
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.
89 90 91 |
# File 'lib/puppet/pops/types/types.rb', line 89 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.
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/puppet/pops/types/types.rb', line 109 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, or if this is exactly Any return true if self.class == PAnyType # An empty variant may be assignable to NotUndef[T] if T is assignable to empty variant return _assignable?(o, guard) if is_a?(PNotUndefType) && o.types.empty? !o.types.empty? && o.types.all? { |vt| assignable?(vt, guard) } when POptionalType # Assignable if undef and contained type is assignable assignable?(PUndefType::DEFAULT) && (o.type.nil? || assignable?(o.type)) 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_type, guard = nil) ⇒ Boolean
Returns `true` if this instance is a callable that accepts the given args_type type
155 156 157 |
# File 'lib/puppet/pops/types/types.rb', line 155 def callable?(args_type, guard = nil) args_type.is_a?(PAnyType) && kind_of_callable? && args_type.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`
173 174 175 |
# File 'lib/puppet/pops/types/types.rb', line 173 def callable_args?(callable, guard) false end |
#callable_with?(args, block = nil) ⇒ Boolean
Returns `true` if this instance is a callable that accepts the given args
164 165 166 |
# File 'lib/puppet/pops/types/types.rb', line 164 def callable_with?(args, block = nil) false end |
#check_self_recursion(originator) ⇒ Object
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.
Called from the `PTypeAliasType` when it detects self recursion. The default is to do nothing but some self recursive constructs are illegal such as when a `PObjectType` somehow inherits itself
182 183 |
# File 'lib/puppet/pops/types/types.rb', line 182 def check_self_recursion(originator) end |
#create(*args) ⇒ Object
317 318 319 |
# File 'lib/puppet/pops/types/types.rb', line 317 def create(*args) Loaders.find_loader(nil).load(:function, 'new').call({}, self, *args) end |
#eql?(o) ⇒ Boolean
280 281 282 |
# File 'lib/puppet/pops/types/types.rb', line 280 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.
190 191 192 193 |
# File 'lib/puppet/pops/types/types.rb', line 190 def generalize # Applicable to all types that have no variables self end |
#hash ⇒ Object
259 260 261 |
# File 'lib/puppet/pops/types/types.rb', line 259 def hash self.class.hash end |
#instance?(o, guard = nil) ⇒ Boolean
Returns true if the given argument o is an instance of this type
267 268 269 |
# File 'lib/puppet/pops/types/types.rb', line 267 def instance?(o, guard = nil) 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`
239 240 241 |
# File 'lib/puppet/pops/types/types.rb', line 239 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?`.
255 256 257 |
# File 'lib/puppet/pops/types/types.rb', line 255 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.
229 230 231 |
# File 'lib/puppet/pops/types/types.rb', line 229 def kind_of_callable?(optional = true, guard = nil) false end |
#loader ⇒ Loaders::Loader
Returns the loader that loaded this type.
197 198 199 |
# File 'lib/puppet/pops/types/types.rb', line 197 def loader Loaders.static_loader end |
#name ⇒ String
Returns the name of the type, without parameters
313 314 315 |
# File 'lib/puppet/pops/types/types.rb', line 313 def name simple_name end |
#new_function ⇒ Function
Create an instance of this type. The default implementation will just dispatch the call to the class method with the same name and pass `self` as the first argument.
328 329 330 |
# File 'lib/puppet/pops/types/types.rb', line 328 def new_function self.class.new_function(self) end |
#normalize(guard = nil) ⇒ PAnyType
208 209 210 |
# File 'lib/puppet/pops/types/types.rb', line 208 def normalize(guard = nil) self end |
#really_instance?(o, guard = nil) ⇒ Integer
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.
An object is considered to really be an instance of a type when something other than a TypeAlias or a Variant responds true to a call to #instance?.
276 277 278 |
# File 'lib/puppet/pops/types/types.rb', line 276 def really_instance?(o, guard = nil) instance?(o, guard) ? 1 : -1 end |
#resolve(loader) ⇒ PTypeAliasType
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.
Called from the TypeParser once it has found a type using the Loader to enable that this type can resolve internal type expressions using a loader. Presently, this method is a no-op for all types except the {PTypeAliasType}.
219 220 221 |
# File 'lib/puppet/pops/types/types.rb', line 219 def resolve(loader) self end |
#roundtrip_with_string? ⇒ Boolean
Answers the question if instances of this type can represent themselves as a string that can then be passed to the create method
349 350 351 |
# File 'lib/puppet/pops/types/types.rb', line 349 def roundtrip_with_string? false end |
#simple_name ⇒ Object
288 289 290 |
# File 'lib/puppet/pops/types/types.rb', line 288 def simple_name self.class.simple_name end |
#to_alias_expanded_s ⇒ Object
302 303 304 |
# File 'lib/puppet/pops/types/types.rb', line 302 def TypeFormatter.new.(self) end |
#to_s ⇒ Object
306 307 308 |
# File 'lib/puppet/pops/types/types.rb', line 306 def to_s TypeFormatter.string(self) end |