class Puppet::Pops::Types::PArrayType

@api public

Constants

DEFAULT
EMPTY

Attributes

element_type[R]

Public Class Methods

new(element_type, size_type = nil) click to toggle source
     # File lib/puppet/pops/types/types.rb
2520 def initialize(element_type, size_type = nil)
2521   super(size_type)
2522   if !size_type.nil? && size_type.from == 0 && size_type.to == 0
2523     @element_type = PUnitType::DEFAULT
2524   else
2525     @element_type = element_type.nil? ? PAnyType::DEFAULT : element_type
2526   end
2527 end
new_function(type) click to toggle source

Returns a new function that produces an Array

     # File lib/puppet/pops/types/types.rb
2587 def self.new_function(type)
2588   @new_function ||= Puppet::Functions.create_loaded_function(:new_array, type.loader) do
2589 
2590     dispatch :to_array do
2591       param           'Variant[Array,Hash,Binary,Iterable]', :from
2592       optional_param  'Boolean[false]', :wrap
2593     end
2594 
2595     dispatch :wrapped do
2596       param  'Any',           :from
2597       param  'Boolean[true]', :wrap
2598     end
2599 
2600     argument_mismatch :on_error do
2601       param  'Any',             :from
2602       optional_param 'Boolean', :wrap
2603     end
2604 
2605     def wrapped(from, _)
2606       from.is_a?(Array) ? from : [from]
2607     end
2608 
2609     def to_array(from, _ = false)
2610       case from
2611       when Array
2612         from
2613       when Hash
2614         from.to_a
2615       when PBinaryType::Binary
2616         # For older rubies, the #bytes method returns an Enumerator that must be rolled out
2617         from.binary_buffer.bytes.to_a
2618       else
2619         Iterable.on(from).to_a
2620       end
2621     end
2622 
2623     def on_error(from, _ = false)
2624       t = TypeCalculator.singleton.infer(from).generalize
2625       _("Value of type %{type} cannot be converted to Array") % { type: t }
2626     end
2627   end
2628 end
register_ptype(loader, ir) click to toggle source
     # File lib/puppet/pops/types/types.rb
2509 def self.register_ptype(loader, ir)
2510   create_ptype(loader, ir, 'CollectionType',
2511     'element_type' => {
2512       KEY_TYPE => POptionalType.new(PTypeType::DEFAULT),
2513       KEY_VALUE => PAnyType::DEFAULT
2514     }
2515   )
2516 end

Public Instance Methods

accept(visitor, guard) click to toggle source
     # File lib/puppet/pops/types/types.rb
2529 def accept(visitor, guard)
2530   super
2531   @element_type.accept(visitor, guard)
2532 end
callable_args?(callable, guard = nil) click to toggle source

@api private

     # File lib/puppet/pops/types/types.rb
2535 def callable_args?(callable, guard = nil)
2536   param_t = callable.param_types
2537   block_t = callable.block_type
2538   # does not support calling with a block, but have to check that callable is ok with missing block
2539   (param_t.nil? || param_t.assignable?(self, guard)) && (block_t.nil? || block_t.assignable?(PUndefType::DEFAULT, guard))
2540 end
eql?(o) click to toggle source
     # File lib/puppet/pops/types/types.rb
2551 def eql?(o)
2552   super && @element_type == o.element_type
2553 end
generalize() click to toggle source
     # File lib/puppet/pops/types/types.rb
2542 def generalize
2543   if PAnyType::DEFAULT.eql?(@element_type)
2544     DEFAULT
2545   else
2546     ge_type = @element_type.generalize
2547     @size_type.nil? && @element_type.equal?(ge_type) ? self : self.class.new(ge_type, nil)
2548   end
2549 end
hash() click to toggle source
     # File lib/puppet/pops/types/types.rb
2555 def hash
2556   super ^ @element_type.hash
2557 end
instance?(o, guard = nil) click to toggle source
     # File lib/puppet/pops/types/types.rb
2573 def instance?(o, guard = nil)
2574   # The inferred type of a class derived from Array is either Runtime or Object. It's not assignable to the Array type.
2575   return false unless o.instance_of?(Array)
2576   return false unless o.all? {|element| @element_type.instance?(element, guard) }
2577   size_t = size_type
2578   size_t.nil? || size_t.instance?(o.size, guard)
2579 end
iterable_type(guard = nil) click to toggle source
     # File lib/puppet/pops/types/types.rb
2581 def iterable_type(guard = nil)
2582   PAnyType::DEFAULT.eql?(@element_type) ? PIterableType::DEFAULT : PIterableType.new(@element_type)
2583 end
normalize(guard = nil) click to toggle source
     # File lib/puppet/pops/types/types.rb
2559 def normalize(guard = nil)
2560   if PAnyType::DEFAULT.eql?(@element_type)
2561     DEFAULT
2562   else
2563     ne_type = @element_type.normalize(guard)
2564     @element_type.equal?(ne_type) ? self : self.class.new(ne_type, @size_type)
2565   end
2566 end
on_error(from, _ = false) click to toggle source
     # File lib/puppet/pops/types/types.rb
2623 def on_error(from, _ = false)
2624   t = TypeCalculator.singleton.infer(from).generalize
2625   _("Value of type %{type} cannot be converted to Array") % { type: t }
2626 end
resolve(loader) click to toggle source
     # File lib/puppet/pops/types/types.rb
2568 def resolve(loader)
2569   relement_type = @element_type.resolve(loader)
2570   relement_type.equal?(@element_type) ? self : self.class.new(relement_type, @size_type)
2571 end
to_array(from, _ = false) click to toggle source
     # File lib/puppet/pops/types/types.rb
2609 def to_array(from, _ = false)
2610   case from
2611   when Array
2612     from
2613   when Hash
2614     from.to_a
2615   when PBinaryType::Binary
2616     # For older rubies, the #bytes method returns an Enumerator that must be rolled out
2617     from.binary_buffer.bytes.to_a
2618   else
2619     Iterable.on(from).to_a
2620   end
2621 end
wrapped(from, _) click to toggle source
     # File lib/puppet/pops/types/types.rb
2605 def wrapped(from, _)
2606   from.is_a?(Array) ? from : [from]
2607 end

Protected Instance Methods

_assignable?(o, guard) click to toggle source

Array is assignable if o is an Array and o's element type is assignable, or if o is a Tuple @api private

Calls superclass method Puppet::Pops::Types::PCollectionType#_assignable?
     # File lib/puppet/pops/types/types.rb
2637 def _assignable?(o, guard)
2638   if o.is_a?(PTupleType)
2639     o_types = o.types
2640     size_s = size_type || DEFAULT_SIZE
2641     size_o = o.size_type
2642     if size_o.nil?
2643       type_count = o_types.size
2644       size_o = PIntegerType.new(type_count, type_count)
2645     end
2646     size_s.assignable?(size_o) && o_types.all? { |ot| @element_type.assignable?(ot, guard) }
2647   elsif o.is_a?(PArrayType)
2648     super && @element_type.assignable?(o.element_type, guard)
2649   else
2650     false
2651   end
2652 end