class Puppet::Pops::Types::PTupleType

@api public

Constants

DEFAULT

Attributes

size_type[R]

If set, describes min and max required of the given types - if max > size of types, the last type entry repeats

types[R]

Public Class Methods

new(types, size_type = nil) click to toggle source
     # File lib/puppet/pops/types/types.rb
2202 def initialize(types, size_type = nil)
2203   @types = types
2204   @size_type = size_type.nil? ? nil : size_type.to_size
2205 end
register_ptype(loader, ir) click to toggle source
     # File lib/puppet/pops/types/types.rb
2146 def self.register_ptype(loader, ir)
2147   create_ptype(loader, ir, 'AnyType',
2148     'types' => PArrayType.new(PTypeType::DEFAULT),
2149     'size_type' => {
2150       KEY_TYPE => POptionalType.new(PTypeType.new(PIntegerType::DEFAULT)),
2151       KEY_VALUE => nil
2152     }
2153   )
2154 end

Public Instance Methods

accept(visitor, guard) click to toggle source
Calls superclass method Puppet::Pops::Types::PAnyType#accept
     # File lib/puppet/pops/types/types.rb
2163 def accept(visitor, guard)
2164   super
2165   @size_type.accept(visitor, guard) unless @size_type.nil?
2166   @types.each { |elem| elem.accept(visitor, guard) }
2167 end
callable_args?(callable_t, guard) click to toggle source

@api private

     # File lib/puppet/pops/types/types.rb
2170 def callable_args?(callable_t, guard)
2171   unless size_type.nil?
2172     raise ArgumentError, 'Callable tuple may not have a size constraint when used as args'
2173   end
2174 
2175   params_tuple = callable_t.param_types
2176   param_block_t = callable_t.block_type
2177   arg_types = @types
2178   arg_block_t = arg_types.last
2179   if arg_block_t.kind_of_callable?(true, guard)
2180     # Can't pass a block to a callable that doesn't accept one
2181     return false if param_block_t.nil?
2182 
2183     # Check that the block is of the right tyṕe
2184     return false unless param_block_t.assignable?(arg_block_t, guard)
2185 
2186     # Check other arguments
2187     arg_count = arg_types.size - 1
2188     params_size_t = params_tuple.size_type || PIntegerType.new(*params_tuple.size_range)
2189     return false unless params_size_t.assignable?(PIntegerType.new(arg_count, arg_count), guard)
2190 
2191     ctypes = params_tuple.types
2192     arg_count.times do |index|
2193       return false unless (ctypes[index] || ctypes[-1]).assignable?(arg_types[index], guard)
2194     end
2195     return true
2196   end
2197 
2198   # Check that tuple is assignable and that the block (if declared) is optional
2199   params_tuple.assignable?(self, guard) && (param_block_t.nil? || param_block_t.assignable?(PUndefType::DEFAULT, guard))
2200 end
each() { |x| ... } click to toggle source

Returns Enumerator for the types if no block is given, otherwise, calls the given block with each of the types in this tuple

     # File lib/puppet/pops/types/types.rb
2209 def each
2210   if block_given?
2211     types.each { |x| yield x }
2212   else
2213     types.to_enum
2214   end
2215 end
eql?(o) click to toggle source
     # File lib/puppet/pops/types/types.rb
2294 def eql?(o)
2295   self.class == o.class && @types == o.types && @size_type == o.size_type
2296 end
generalize() click to toggle source
     # File lib/puppet/pops/types/types.rb
2217 def generalize
2218   if self == DEFAULT
2219     DEFAULT
2220   else
2221     alter_type_array(@types, :generalize) { |altered_types| PTupleType.new(altered_types, @size_type) }
2222   end
2223 end
hash() click to toggle source
     # File lib/puppet/pops/types/types.rb
2290 def hash
2291   @size_type.hash ^ @types.hash
2292 end
instance?(o, guard = nil) click to toggle source
     # File lib/puppet/pops/types/types.rb
2243 def instance?(o, guard = nil)
2244   # The inferred type of a class derived from Array is either Runtime or Object. It's not assignable to the Tuple type.
2245   return false unless o.instance_of?(Array)
2246   if @size_type
2247     return false unless @size_type.instance?(o.size, guard)
2248   else
2249     return false unless @types.empty? || @types.size == o.size
2250   end
2251   index = -1
2252   @types.empty? || o.all? do |element|
2253     @types.fetch(index += 1) { @types.last }.instance?(element, guard)
2254   end
2255 end
iterable?(guard = nil) click to toggle source
     # File lib/puppet/pops/types/types.rb
2257 def iterable?(guard = nil)
2258   true
2259 end
iterable_type(guard = nil) click to toggle source
     # File lib/puppet/pops/types/types.rb
2261 def iterable_type(guard = nil)
2262   PIterableType.new(PVariantType.maybe_create(types))
2263 end
new_function() click to toggle source
     # File lib/puppet/pops/types/types.rb
2298 def new_function
2299   # Simply delegate to Array type and let the higher level assertion deal with
2300   # compliance with the Tuple type regarding the produced result.
2301   PArrayType.new_function(self)
2302 end
normalize(guard = nil) click to toggle source
     # File lib/puppet/pops/types/types.rb
2225 def normalize(guard = nil)
2226   if self == DEFAULT
2227     DEFAULT
2228   else
2229     alter_type_array(@types, :normalize, guard) { |altered_types| PTupleType.new(altered_types, @size_type) }
2230   end
2231 end
repeat_last_range() click to toggle source

Returns the number of accepted occurrences [min, max] of the last type in the tuple The defaults is [1,1]

     # File lib/puppet/pops/types/types.rb
2278 def repeat_last_range
2279   if @size_type.nil?
2280     return [1, 1]
2281   end
2282   types_size = @types.size
2283   from, to = @size_type.range
2284   min = from - (types_size-1)
2285   min = min <= 0 ? 0 : min
2286   max = to - (types_size-1)
2287   [min, max]
2288 end
resolve(loader) click to toggle source
     # File lib/puppet/pops/types/types.rb
2233 def resolve(loader)
2234   changed = false
2235   rtypes = @types.map do |type|
2236     rtype = type.resolve(loader)
2237     changed ||= !rtype.equal?(type)
2238     rtype
2239   end
2240   changed ? self.class.new(rtypes, @size_type) : self
2241 end
size_range() click to toggle source

Returns the number of elements accepted [min, max] in the tuple

     # File lib/puppet/pops/types/types.rb
2266 def size_range
2267   if @size_type.nil?
2268     types_size = @types.size
2269     types_size == 0 ? [0, Float::INFINITY] : [types_size, types_size]
2270   else
2271     @size_type.range
2272   end
2273 end

Protected Instance Methods

_assignable?(o, guard) click to toggle source

@api private

     # File lib/puppet/pops/types/types.rb
2309 def _assignable?(o, guard)
2310   return true if self == o
2311   return false unless o.is_a?(PTupleType) || o.is_a?(PArrayType)
2312   s_types = types
2313   size_s = size_type || PIntegerType.new(*size_range)
2314 
2315   if o.is_a?(PTupleType)
2316     size_o = o.size_type || PIntegerType.new(*o.size_range)
2317     return false unless size_s.assignable?(size_o, guard)
2318     unless s_types.empty?
2319       o_types = o.types
2320       return size_s.numeric_from == 0 if o_types.empty?
2321       o_types.size.times do |index|
2322         return false unless (s_types[index] || s_types[-1]).assignable?(o_types[index], guard)
2323       end
2324     end
2325   else
2326     size_o = o.size_type || PCollectionType::DEFAULT_SIZE
2327     return false unless size_s.assignable?(size_o, guard)
2328     unless s_types.empty?
2329       o_entry = o.element_type
2330       # Array of anything can not be assigned (unless tuple is tuple of anything) - this case
2331       # was handled at the top of this method.
2332       #
2333       return false if o_entry.nil?
2334       [s_types.size, size_o.range[1]].min.times { |index| return false unless (s_types[index] || s_types[-1]).assignable?(o_entry, guard) }
2335     end
2336   end
2337   true
2338 end