Class: Puppet::Pops::Types::PCallableType

Inherits:
PAnyType show all
Defined in:
lib/puppet/pops/types/types.rb

Constant Summary collapse

DEFAULT =
PCallableType.new(nil, nil, nil)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PAnyType

#==, #assignable?, #callable?, #check_self_recursion, create, #create, #iterable?, #iterable_type, #loader, #name, new_function, #new_function, #really_instance?, #roundtrip_with_string?, simple_name, #simple_name, #to_alias_expanded_s, #to_s

Methods inherited from TypedModelObject

_pcore_type, create_ptype, register_ptypes

Methods included from PuppetObject

#_pcore_all_contents, #_pcore_contents, #_pcore_init_hash, #_pcore_type, #to_s

Constructor Details

#initialize(param_types, block_type = nil, return_type = nil) ⇒ PCallableType

Returns a new instance of PCallableType.

Parameters:



2392
2393
2394
2395
2396
# File 'lib/puppet/pops/types/types.rb', line 2392

def initialize(param_types, block_type = nil, return_type = nil)
  @param_types = param_types
  @block_type = block_type
  @return_type = return_type == PAnyType::DEFAULT ? nil : return_type
end

Instance Attribute Details

#block_typePAnyType|nil (readonly)

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.

Returns:



2387
2388
2389
# File 'lib/puppet/pops/types/types.rb', line 2387

def block_type
  @block_type
end

#param_typesPTupleType (readonly)

Types of parameters as a Tuple with required/optional count, or an Integer with min (required), max count

Returns:

  • (PTupleType)

    the tuple representing the parameter types



2381
2382
2383
# File 'lib/puppet/pops/types/types.rb', line 2381

def param_types
  @param_types
end

#return_typePAnyType (readonly)

Returns The type for the values returned by this callable. Returns ‘nil` if return value is unconstrained.

Returns:

  • (PAnyType)

    The type for the values returned by this callable. Returns ‘nil` if return value is unconstrained



2377
2378
2379
# File 'lib/puppet/pops/types/types.rb', line 2377

def return_type
  @return_type
end

Class Method Details

.register_ptype(loader, ir) ⇒ Object



2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
# File 'lib/puppet/pops/types/types.rb', line 2360

def self.register_ptype(loader, ir)
  create_ptype(loader, ir, 'AnyType',
               'param_types' => {
                 KEY_TYPE => POptionalType.new(PTypeType.new(PTupleType::DEFAULT)),
                 KEY_VALUE => nil
               },
               'block_type' => {
                 KEY_TYPE => POptionalType.new(PTypeType.new(PCallableType::DEFAULT)),
                 KEY_VALUE => nil
               },
               'return_type' => {
                 KEY_TYPE => POptionalType.new(PTypeType::DEFAULT),
                 KEY_VALUE => PAnyType::DEFAULT
               })
end

Instance Method Details

#accept(visitor, guard) ⇒ Object



2398
2399
2400
2401
2402
2403
# File 'lib/puppet/pops/types/types.rb', line 2398

def accept(visitor, guard)
  super
  @param_types.accept(visitor, guard) unless @param_types.nil?
  @block_type.accept(visitor, guard) unless @block_type.nil?
  @return_type.accept(visitor, guard) unless @return_type.nil?
end

#block_rangeObject

Range [0,0], [0,1], or [1,1] for the block



2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
# File 'lib/puppet/pops/types/types.rb', line 2470

def block_range
  case block_type
  when POptionalType
    [0, 1]
  when PVariantType, PCallableType
    [1, 1]
  else
    [0, 0]
  end
end

#callable_args?(required_callable_t, 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:

  • (Boolean)


2448
2449
2450
2451
# File 'lib/puppet/pops/types/types.rb', line 2448

def callable_args?(required_callable_t, guard)
  # If the required callable is equal or more specific than self, self is acceptable arguments
  required_callable_t.assignable?(self, guard)
end

#callable_with?(args, block = nil) ⇒ Boolean

Returns ‘true` if this instance is a callable that accepts the given args

Parameters:

  • args (Array)

    the arguments to test

Returns:

  • (Boolean)

    ‘true` if this instance is a callable that accepts the given args



2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
# File 'lib/puppet/pops/types/types.rb', line 2435

def callable_with?(args, block = nil)
  # nil param_types and compatible return type means other Callable is assignable
  return true if @param_types.nil?
  return false unless @param_types.instance?(args)

  if @block_type.nil?
    block.nil?
  else
    @block_type.instance?(block)
  end
end

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)


2485
2486
2487
# File 'lib/puppet/pops/types/types.rb', line 2485

def eql?(o)
  self.class == o.class && @param_types == o.param_types && @block_type == o.block_type && @return_type == o.return_type
end

#generalizeObject



2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
# File 'lib/puppet/pops/types/types.rb', line 2405

def generalize
  if self == DEFAULT
    DEFAULT
  else
    params_t = @param_types.nil? ? nil : @param_types.generalize
    block_t = @block_type.nil? ? nil : @block_type.generalize
    return_t = @return_type.nil? ? nil : @return_type.generalize
    @param_types.equal?(params_t) && @block_type.equal?(block_t) && @return_type.equal?(return_t) ? self : PCallableType.new(params_t, block_t, return_t)
  end
end

#hashObject



2481
2482
2483
# File 'lib/puppet/pops/types/types.rb', line 2481

def hash
  [@param_types, @block_type, @return_type].hash
end

#instance?(o, guard = nil) ⇒ Boolean

Returns:

  • (Boolean)


2427
2428
2429
# File 'lib/puppet/pops/types/types.rb', line 2427

def instance?(o, guard = nil)
  (o.is_a?(Proc) || o.is_a?(Evaluator::Closure) || o.is_a?(Functions::Function)) && assignable?(TypeCalculator.infer(o), guard)
end

#kind_of_callable?(optional = true, guard = nil) ⇒ Boolean

Returns:

  • (Boolean)


2453
2454
2455
# File 'lib/puppet/pops/types/types.rb', line 2453

def kind_of_callable?(optional = true, guard = nil)
  true
end

#last_rangeObject

Returns the number of accepted arguments for the last parameter type [min, max]



2464
2465
2466
# File 'lib/puppet/pops/types/types.rb', line 2464

def last_range
  @param_types.nil? ? nil : @param_types.repeat_last_range
end

#normalize(guard = nil) ⇒ Object



2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
# File 'lib/puppet/pops/types/types.rb', line 2416

def normalize(guard = nil)
  if self == DEFAULT
    DEFAULT
  else
    params_t = @param_types.nil? ? nil : @param_types.normalize(guard)
    block_t = @block_type.nil? ? nil : @block_type.normalize(guard)
    return_t = @return_type.nil? ? nil : @return_type.normalize(guard)
    @param_types.equal?(params_t) && @block_type.equal?(block_t) && @return_type.equal?(return_t) ? self : PCallableType.new(params_t, block_t, return_t)
  end
end

#resolve(loader) ⇒ Object



2489
2490
2491
2492
2493
2494
# File 'lib/puppet/pops/types/types.rb', line 2489

def resolve(loader)
  params_t = @param_types.nil? ? nil : @param_types.resolve(loader)
  block_t = @block_type.nil? ? nil : @block_type.resolve(loader)
  return_t = @return_type.nil? ? nil : @return_type.resolve(loader)
  @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)
end

#size_rangeObject

Returns the number of accepted arguments [min, max]



2458
2459
2460
# File 'lib/puppet/pops/types/types.rb', line 2458

def size_range
  @param_types.nil? ? nil : @param_types.size_range
end