{"record":{"id":"4d01ad848da4af59","repo":"puppetlabs/puppet","slug":"creation-of-new-instance-of-type-type-name-is","errorCode":null,"errorMessage":"Creation of new instance of type '%{type_name}' is not supported","messagePattern":"Creation of new instance of type '%(.+?)' is not supported","errorType":"exception","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"lib/puppet/pops/types/p_init_type.rb","lineNumber":160,"sourceCode":"  def assert_initialized\n    return self if @initialized\n\n    @initialized = true\n    @self_recursion = true\n\n    begin\n      # Filter out types that will provide a new_function but are unsuitable to be contained in Init\n      #\n      # Calling Init#new would cause endless recursion\n      # The Optional is the same as Variant[T,Undef].\n      # The NotUndef is not meaningful to create instances of\n      if @type.instance_of?(PInitType) || @type.instance_of?(POptionalType) || @type.instance_of?(PNotUndefType)\n        raise ArgumentError\n      end\n\n      new_func = @type.new_function\n    rescue ArgumentError\n      raise ArgumentError, _(\"Creation of new instance of type '%{type_name}' is not supported\") % { type_name: @type.to_s }\n    end\n    param_tuples = new_func.dispatcher.signatures.map { |closure| closure.type.param_types }\n\n    # An instance of the contained type is always a match to this type.\n    single_types = [@type]\n\n    if @init_args.empty?\n      # A value that is assignable to the type of a single parameter is also a match\n      single_tuples, other_tuples = param_tuples.partition { |tuple| EXACTLY_ONE == tuple.size_range }\n      single_types.concat(single_tuples.map { |tuple| tuple.types[0] })\n    else\n      tc = TypeCalculator.singleton\n      init_arg_types = @init_args.map { |arg| tc.infer_set(arg) }\n      arg_count = 1 + init_arg_types.size\n\n      # disqualify all parameter tuples that doesn't allow one value (type unknown at ths stage) + init args.\n      param_tuples = param_tuples.select do |tuple|\n        min, max = tuple.size_range","sourceCodeStart":142,"sourceCodeEnd":178,"githubUrl":"https://github.com/puppetlabs/puppet/blob/e227c27540975c25aa22d533a52424a9d2fc886a/lib/puppet/pops/types/p_init_type.rb#L142-L178","documentation":"Computing the constructor of Init[T] (used by Init[T].new(...)) filters out types that cannot be meaningfully instantiated: Optional and NotUndef are wrapper types, and a nested Init would recurse endlessly when creating instances. Types whose new_function also raises ArgumentError fall into the same rescue, which re-raises with this clearer message naming the offending type.","triggerScenarios":"Instantiating Init[Optional[String]].new('x'), Init[NotUndef[Integer]].new(3), or Init[Init[String]].new('x') in Puppet DSL; or calling new_function on such a PInitType from Ruby (e.g. generic code that wraps inferred types in Init).","commonSituations":"Code that programmatically wraps arbitrary inferred types in Init and later instantiates them (TypeCalculator results often contain Optional); template libraries that 'add defaults' by wrapping every type in Init; copying Init examples from type-algebra docs into constructors.","solutions":["Parameterize the inner type directly: use Init[String] instead of Init[Optional[String]].","Strip Optional/NotUndef/Init wrappers before building the Init type: unwrap to the contained type first.","If you wrap types generically, skip wrapping when the type is Optional/NotUndef/Init (or any type without a new_function)."],"exampleFix":"# before\n$v = Init[Optional[String]].new('hello')\n\n# after\n$v = Init[String].new('hello')","handlingStrategy":"type-guard","validationCode":"# Ruby — skip wrapper types when generating Init types\nWRAPPERS = [Puppet::Pops::Types::PInitType, Puppet::Pops::Types::POptionalType, Puppet::Pops::Types::PNotUndefType]\ninner = WRAPPERS.any? { |w| t.instance_of?(w) } ? nil : t","typeGuard":"def init_constructible?(t)\n  !t.instance_of?(Puppet::Pops::Types::PInitType) &&\n    !t.instance_of?(Puppet::Pops::Types::POptionalType) &&\n    !t.instance_of?(Puppet::Pops::Types::PNotUndefType)\nend","tryCatchPattern":"begin\n  init_t.new_function\nrescue ArgumentError => e\n  raise TypeError, \"cannot instantiate #{init_t}: #{e.message}\" # or fall back to the inner type\nend","preventionTips":["Unwrap Optional/NotUndef/Init before parameterizing Init with the contained type.","In manifests, remember Optional[T]/NotUndef[T] describe matching, not construction."],"tags":["puppet","pcore","init-type","object-construction","type-system"],"backgroundTag":"type-not-constructible","analyzedSha":"e227c27540975c25aa22d533a52424a9d2fc886a","analyzedAt":"2026-08-21T20:49:46.650Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}