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, #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

Constructor Details

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

Returns a new instance of PCallableType.

Parameters:



2197
2198
2199
2200
2201
# File 'lib/puppet/pops/types/types.rb', line 2197

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:



2192
2193
2194
# File 'lib/puppet/pops/types/types.rb', line 2192

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



2186
2187
2188
# File 'lib/puppet/pops/types/types.rb', line 2186

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



2182
2183
2184
# File 'lib/puppet/pops/types/types.rb', line 2182

def return_type
  @return_type
end

Class Method Details

.register_ptype(loader, ir) ⇒ Object



2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
# File 'lib/puppet/pops/types/types.rb', line 2164

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

Instance Method Details

#accept(visitor, guard) ⇒ Object



2203
2204
2205
2206
2207
2208
# File 'lib/puppet/pops/types/types.rb', line 2203

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



2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
# File 'lib/puppet/pops/types/types.rb', line 2274

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)


2252
2253
2254
2255
# File 'lib/puppet/pops/types/types.rb', line 2252

def callable_args?(required_callable_t, guard)
  # If the required callable is euqal 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



2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
# File 'lib/puppet/pops/types/types.rb', line 2240

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)


2289
2290
2291
# File 'lib/puppet/pops/types/types.rb', line 2289

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

#generalizeObject



2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
# File 'lib/puppet/pops/types/types.rb', line 2210

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



2285
2286
2287
# File 'lib/puppet/pops/types/types.rb', line 2285

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

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

Returns:

  • (Boolean)


2232
2233
2234
# File 'lib/puppet/pops/types/types.rb', line 2232

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)


2257
2258
2259
# File 'lib/puppet/pops/types/types.rb', line 2257

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]



2268
2269
2270
# File 'lib/puppet/pops/types/types.rb', line 2268

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

#normalize(guard = nil) ⇒ Object



2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
# File 'lib/puppet/pops/types/types.rb', line 2221

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(type_parser, loader) ⇒ Object



2293
2294
2295
2296
2297
2298
# File 'lib/puppet/pops/types/types.rb', line 2293

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



2262
2263
2264
# File 'lib/puppet/pops/types/types.rb', line 2262

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