class Puppet::Pops::Types::PCallableType
@api public
Constants
- DEFAULT
Attributes
Although being an abstract type reference, only Callable, or all Callables wrapped in Optional or Variant are supported If not set, the meaning is that block is not supported. @return [PAnyType|nil] the block type
Types
of parameters as a Tuple with required/optional count, or an Integer with min (required), max count @return [PTupleType] the tuple representing the parameter types
@return [PAnyType] The type for the values returned by this callable. Returns `nil` if return value is unconstrained
Public Class Methods
@param param_types
[PTupleType] @param block_type
[PAnyType] @param return_type
[PAnyType]
# File lib/puppet/pops/types/types.rb 2377 def initialize(param_types, block_type = nil, return_type = nil) 2378 @param_types = param_types 2379 @block_type = block_type 2380 @return_type = return_type == PAnyType::DEFAULT ? nil : return_type 2381 end
# File lib/puppet/pops/types/types.rb 2344 def self.register_ptype(loader, ir) 2345 create_ptype(loader, ir, 'AnyType', 2346 'param_types' => { 2347 KEY_TYPE => POptionalType.new(PTypeType.new(PTupleType::DEFAULT)), 2348 KEY_VALUE => nil 2349 }, 2350 'block_type' => { 2351 KEY_TYPE => POptionalType.new(PTypeType.new(PCallableType::DEFAULT)), 2352 KEY_VALUE => nil 2353 }, 2354 'return_type' => { 2355 KEY_TYPE => POptionalType.new(PTypeType::DEFAULT), 2356 KEY_VALUE => PAnyType::DEFAULT 2357 } 2358 ) 2359 end
Public Instance Methods
Puppet::Pops::Types::PAnyType#accept
# File lib/puppet/pops/types/types.rb 2383 def accept(visitor, guard) 2384 super 2385 @param_types.accept(visitor, guard) unless @param_types.nil? 2386 @block_type.accept(visitor, guard) unless @block_type.nil? 2387 @return_type.accept(visitor, guard) unless @return_type.nil? 2388 end
Range [0,0], [0,1], or [1,1] for the block
# File lib/puppet/pops/types/types.rb 2454 def block_range 2455 case block_type 2456 when POptionalType 2457 [0,1] 2458 when PVariantType, PCallableType 2459 [1,1] 2460 else 2461 [0,0] 2462 end 2463 end
@api private
# File lib/puppet/pops/types/types.rb 2432 def callable_args?(required_callable_t, guard) 2433 # If the required callable is equal or more specific than self, self is acceptable arguments 2434 required_callable_t.assignable?(self, guard) 2435 end
Returns `true` if this instance is a callable that accepts the given args
@param args [Array] the arguments to test @return [Boolean] `true` if this instance is a callable that accepts the given args
# File lib/puppet/pops/types/types.rb 2420 def callable_with?(args, block = nil) 2421 # nil param_types and compatible return type means other Callable is assignable 2422 return true if @param_types.nil? 2423 return false unless @param_types.instance?(args) 2424 if @block_type.nil? 2425 block == nil 2426 else 2427 @block_type.instance?(block) 2428 end 2429 end
# File lib/puppet/pops/types/types.rb 2469 def eql?(o) 2470 self.class == o.class && @param_types == o.param_types && @block_type == o.block_type && @return_type == o.return_type 2471 end
# File lib/puppet/pops/types/types.rb 2390 def generalize 2391 if self == DEFAULT 2392 DEFAULT 2393 else 2394 params_t = @param_types.nil? ? nil : @param_types.generalize 2395 block_t = @block_type.nil? ? nil : @block_type.generalize 2396 return_t = @return_type.nil? ? nil : @return_type.generalize 2397 @param_types.equal?(params_t) && @block_type.equal?(block_t) && @return_type.equal?(return_t) ? self : PCallableType.new(params_t, block_t, return_t) 2398 end 2399 end
# File lib/puppet/pops/types/types.rb 2465 def hash 2466 [@param_types, @block_type, @return_type].hash 2467 end
# File lib/puppet/pops/types/types.rb 2412 def instance?(o, guard = nil) 2413 (o.is_a?(Proc) || o.is_a?(Evaluator::Closure) || o.is_a?(Functions::Function)) && assignable?(TypeCalculator.infer(o), guard) 2414 end
# File lib/puppet/pops/types/types.rb 2437 def kind_of_callable?(optional=true, guard = nil) 2438 true 2439 end
Returns the number of accepted arguments for the last parameter type [min, max]
# File lib/puppet/pops/types/types.rb 2448 def last_range 2449 @param_types.nil? ? nil : @param_types.repeat_last_range 2450 end
# File lib/puppet/pops/types/types.rb 2401 def normalize(guard = nil) 2402 if self == DEFAULT 2403 DEFAULT 2404 else 2405 params_t = @param_types.nil? ? nil : @param_types.normalize(guard) 2406 block_t = @block_type.nil? ? nil : @block_type.normalize(guard) 2407 return_t = @return_type.nil? ? nil : @return_type.normalize(guard) 2408 @param_types.equal?(params_t) && @block_type.equal?(block_t) && @return_type.equal?(return_t) ? self : PCallableType.new(params_t, block_t, return_t) 2409 end 2410 end
# File lib/puppet/pops/types/types.rb 2473 def resolve(loader) 2474 params_t = @param_types.nil? ? nil : @param_types.resolve(loader) 2475 block_t = @block_type.nil? ? nil : @block_type.resolve(loader) 2476 return_t = @return_type.nil? ? nil : @return_type.resolve(loader) 2477 @param_types.equal?(params_t) && @block_type.equal?(block_t) && @return_type.equal?(return_t) ? self : self.class.new(params_t, block_t, return_t) 2478 end
Returns the number of accepted arguments [min, max]
# File lib/puppet/pops/types/types.rb 2442 def size_range 2443 @param_types.nil? ? nil : @param_types.size_range 2444 end
Protected Instance Methods
@api private
# File lib/puppet/pops/types/types.rb 2485 def _assignable?(o, guard) 2486 return false unless o.is_a?(PCallableType) 2487 return false unless @return_type.nil? || @return_type.assignable?(o.return_type || PAnyType::DEFAULT, guard) 2488 2489 # nil param_types and compatible return type means other Callable is assignable 2490 return true if @param_types.nil? 2491 2492 # NOTE: these tests are made in reverse as it is calling the callable that is constrained 2493 # (it's lower bound), not its upper bound 2494 other_param_types = o.param_types 2495 2496 return false if other_param_types.nil? || !other_param_types.assignable?(@param_types, guard) 2497 # names are ignored, they are just information 2498 # Blocks must be compatible 2499 this_block_t = @block_type || PUndefType::DEFAULT 2500 that_block_t = o.block_type || PUndefType::DEFAULT 2501 that_block_t.assignable?(this_block_t, guard) 2502 end