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
2203 def initialize(types, size_type = nil)
2204   @types = types
2205   @size_type = size_type.nil? ? nil : size_type.to_size
2206 end
register_ptype(loader, ir) click to toggle source
     # File lib/puppet/pops/types/types.rb
2147 def self.register_ptype(loader, ir)
2148   create_ptype(loader, ir, 'AnyType',
2149     'types' => PArrayType.new(PTypeType::DEFAULT),
2150     'size_type' => {
2151       KEY_TYPE => POptionalType.new(PTypeType.new(PIntegerType::DEFAULT)),
2152       KEY_VALUE => nil
2153     }
2154   )
2155 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
2164 def accept(visitor, guard)
2165   super
2166   @size_type.accept(visitor, guard) unless @size_type.nil?
2167   @types.each { |elem| elem.accept(visitor, guard) }
2168 end
callable_args?(callable_t, guard) click to toggle source

@api private

     # File lib/puppet/pops/types/types.rb
2171 def callable_args?(callable_t, guard)
2172   unless size_type.nil?
2173     raise ArgumentError, 'Callable tuple may not have a size constraint when used as args'
2174   end
2175 
2176   params_tuple = callable_t.param_types
2177   param_block_t = callable_t.block_type
2178   arg_types = @types
2179   arg_block_t = arg_types.last
2180   if arg_block_t.kind_of_callable?(true, guard)
2181     # Can't pass a block to a callable that doesn't accept one
2182     return false if param_block_t.nil?
2183 
2184     # Check that the block is of the right tyṕe
2185     return false unless param_block_t.assignable?(arg_block_t, guard)
2186 
2187     # Check other arguments
2188     arg_count = arg_types.size - 1
2189     params_size_t = params_tuple.size_type || PIntegerType.new(*params_tuple.size_range)
2190     return false unless params_size_t.assignable?(PIntegerType.new(arg_count, arg_count), guard)
2191 
2192     ctypes = params_tuple.types
2193     arg_count.times do |index|
2194       return false unless (ctypes[index] || ctypes[-1]).assignable?(arg_types[index], guard)
2195     end
2196     return true
2197   end
2198 
2199   # Check that tuple is assignable and that the block (if declared) is optional
2200   params_tuple.assignable?(self, guard) && (param_block_t.nil? || param_block_t.assignable?(PUndefType::DEFAULT, guard))
2201 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
2210 def each
2211   if block_given?
2212     types.each { |x| yield x }
2213   else
2214     types.to_enum
2215   end
2216 end
eql?(o) click to toggle source
     # File lib/puppet/pops/types/types.rb
2295 def eql?(o)
2296   self.class == o.class && @types == o.types && @size_type == o.size_type
2297 end
generalize() click to toggle source
     # File lib/puppet/pops/types/types.rb
2218 def generalize
2219   if self == DEFAULT
2220     DEFAULT
2221   else
2222     alter_type_array(@types, :generalize) { |altered_types| PTupleType.new(altered_types, @size_type) }
2223   end
2224 end
hash() click to toggle source
     # File lib/puppet/pops/types/types.rb
2291 def hash
2292   @size_type.hash ^ @types.hash
2293 end
instance?(o, guard = nil) click to toggle source
     # File lib/puppet/pops/types/types.rb
2244 def instance?(o, guard = nil)
2245   # The inferred type of a class derived from Array is either Runtime or Object. It's not assignable to the Tuple type.
2246   return false unless o.instance_of?(Array)
2247   if @size_type
2248     return false unless @size_type.instance?(o.size, guard)
2249   else
2250     return false unless @types.empty? || @types.size == o.size
2251   end
2252   index = -1
2253   @types.empty? || o.all? do |element|
2254     @types.fetch(index += 1) { @types.last }.instance?(element, guard)
2255   end
2256 end
iterable?(guard = nil) click to toggle source
     # File lib/puppet/pops/types/types.rb
2258 def iterable?(guard = nil)
2259   true
2260 end
iterable_type(guard = nil) click to toggle source
     # File lib/puppet/pops/types/types.rb
2262 def iterable_type(guard = nil)
2263   PIterableType.new(PVariantType.maybe_create(types))
2264 end
new_function() click to toggle source
     # File lib/puppet/pops/types/types.rb
2299 def new_function
2300   # Simply delegate to Array type and let the higher level assertion deal with
2301   # compliance with the Tuple type regarding the produced result.
2302   PArrayType.new_function(self)
2303 end
normalize(guard = nil) click to toggle source
     # File lib/puppet/pops/types/types.rb
2226 def normalize(guard = nil)
2227   if self == DEFAULT
2228     DEFAULT
2229   else
2230     alter_type_array(@types, :normalize, guard) { |altered_types| PTupleType.new(altered_types, @size_type) }
2231   end
2232 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
2279 def repeat_last_range
2280   if @size_type.nil?
2281     return [1, 1]
2282   end
2283   types_size = @types.size
2284   from, to = @size_type.range
2285   min = from - (types_size-1)
2286   min = min <= 0 ? 0 : min
2287   max = to - (types_size-1)
2288   [min, max]
2289 end
resolve(loader) click to toggle source
     # File lib/puppet/pops/types/types.rb
2234 def resolve(loader)
2235   changed = false
2236   rtypes = @types.map do |type|
2237     rtype = type.resolve(loader)
2238     changed ||= !rtype.equal?(type)
2239     rtype
2240   end
2241   changed ? self.class.new(rtypes, @size_type) : self
2242 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
2267 def size_range
2268   if @size_type.nil?
2269     types_size = @types.size
2270     types_size == 0 ? [0, Float::INFINITY] : [types_size, types_size]
2271   else
2272     @size_type.range
2273   end
2274 end

Protected Instance Methods

_assignable?(o, guard) click to toggle source

@api private

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