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
2521 def initialize(element_type, size_type = nil)
2522   super(size_type)
2523   if !size_type.nil? && size_type.from == 0 && size_type.to == 0
2524     @element_type = PUnitType::DEFAULT
2525   else
2526     @element_type = element_type.nil? ? PAnyType::DEFAULT : element_type
2527   end
2528 end
new_function(type) click to toggle source

Returns a new function that produces an Array

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

Public Instance Methods

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

@api private

     # File lib/puppet/pops/types/types.rb
2536 def callable_args?(callable, guard = nil)
2537   param_t = callable.param_types
2538   block_t = callable.block_type
2539   # does not support calling with a block, but have to check that callable is ok with missing block
2540   (param_t.nil? || param_t.assignable?(self, guard)) && (block_t.nil? || block_t.assignable?(PUndefType::DEFAULT, guard))
2541 end
eql?(o) click to toggle source
     # File lib/puppet/pops/types/types.rb
2552 def eql?(o)
2553   super && @element_type == o.element_type
2554 end
generalize() click to toggle source
     # File lib/puppet/pops/types/types.rb
2543 def generalize
2544   if PAnyType::DEFAULT.eql?(@element_type)
2545     DEFAULT
2546   else
2547     ge_type = @element_type.generalize
2548     @size_type.nil? && @element_type.equal?(ge_type) ? self : self.class.new(ge_type, nil)
2549   end
2550 end
hash() click to toggle source
     # File lib/puppet/pops/types/types.rb
2556 def hash
2557   super ^ @element_type.hash
2558 end
instance?(o, guard = nil) click to toggle source
     # File lib/puppet/pops/types/types.rb
2574 def instance?(o, guard = nil)
2575   # The inferred type of a class derived from Array is either Runtime or Object. It's not assignable to the Array type.
2576   return false unless o.instance_of?(Array)
2577   return false unless o.all? {|element| @element_type.instance?(element, guard) }
2578   size_t = size_type
2579   size_t.nil? || size_t.instance?(o.size, guard)
2580 end
iterable_type(guard = nil) click to toggle source
     # File lib/puppet/pops/types/types.rb
2582 def iterable_type(guard = nil)
2583   PAnyType::DEFAULT.eql?(@element_type) ? PIterableType::DEFAULT : PIterableType.new(@element_type)
2584 end
normalize(guard = nil) click to toggle source
     # File lib/puppet/pops/types/types.rb
2560 def normalize(guard = nil)
2561   if PAnyType::DEFAULT.eql?(@element_type)
2562     DEFAULT
2563   else
2564     ne_type = @element_type.normalize(guard)
2565     @element_type.equal?(ne_type) ? self : self.class.new(ne_type, @size_type)
2566   end
2567 end
on_error(from, _ = false) click to toggle source
     # File lib/puppet/pops/types/types.rb
2624 def on_error(from, _ = false)
2625   t = TypeCalculator.singleton.infer(from).generalize
2626   _("Value of type %{type} cannot be converted to Array") % { type: t }
2627 end
resolve(loader) click to toggle source
     # File lib/puppet/pops/types/types.rb
2569 def resolve(loader)
2570   relement_type = @element_type.resolve(loader)
2571   relement_type.equal?(@element_type) ? self : self.class.new(relement_type, @size_type)
2572 end
to_array(from, _ = false) click to toggle source
     # File lib/puppet/pops/types/types.rb
2610 def to_array(from, _ = false)
2611   case from
2612   when Array
2613     from
2614   when Hash
2615     from.to_a
2616   when PBinaryType::Binary
2617     # For older rubies, the #bytes method returns an Enumerator that must be rolled out
2618     from.binary_buffer.bytes.to_a
2619   else
2620     Iterable.on(from).to_a
2621   end
2622 end
wrapped(from, _) click to toggle source
     # File lib/puppet/pops/types/types.rb
2606 def wrapped(from, _)
2607   from.is_a?(Array) ? from : [from]
2608 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
2638 def _assignable?(o, guard)
2639   if o.is_a?(PTupleType)
2640     o_types = o.types
2641     size_s = size_type || DEFAULT_SIZE
2642     size_o = o.size_type
2643     if size_o.nil?
2644       type_count = o_types.size
2645       size_o = PIntegerType.new(type_count, type_count)
2646     end
2647     size_s.assignable?(size_o) && o_types.all? { |ot| @element_type.assignable?(ot, guard) }
2648   elsif o.is_a?(PArrayType)
2649     super && @element_type.assignable?(o.element_type, guard)
2650   else
2651     false
2652   end
2653 end