{"record":{"id":"37c694c12fe48b23","repo":"apple/pkl","slug":"cannotinstantiatetype","errorCode":"cannotInstantiateType","errorMessage":"Cannot instantiate type `{0}`.","messagePattern":"Cannot instantiate type `(.+?)`\\.","errorType":"error_code","errorClass":"VmException","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/ast/expression/member/AbstractInferParentNode.java","lineNumber":65,"sourceCode":"\n    var defaultValue = typeNode.createDefaultValue(frame, language, headerSection, qualifiedName);\n    if (defaultValue != null) {\n      return defaultValue;\n    }\n\n    CompilerDirectives.transferToInterpreter();\n\n    if (typeNode instanceof TypeVariableNode) {\n      throw exceptionBuilder().evalError(\"cannotInferParent\").build();\n    }\n\n    // try to produce a more specific error message than \"cannotInstantiateType\"\n    var clazz = typeNode.getVmClass();\n    if (clazz != null) {\n      VmUtils.checkIsInstantiable(clazz, typeNode);\n    }\n\n    throw exceptionBuilder()\n        .evalError(\"cannotInstantiateType\", typeNode.getSourceSection().getCharacters())\n        .build();\n  }\n}\n","sourceCodeStart":47,"sourceCodeEnd":70,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/ast/expression/member/AbstractInferParentNode.java#L47-L70","documentation":"Pkl throws `cannotInstantiateType` when `new Type { ... }` names a type that cannot be instantiated — abstract classes, type aliases to non-instantiable types, modules, etc. AbstractInferParentNode.getDefaultValue falls through to this error after the more specific checks fail, including the source text of the type in the message.","triggerScenarios":"`new` applied to an abstract/open class, a module, a union or constrained type where no concrete class is resolvable; typeNode.getVmClass() is null or VmUtils.checkIsInstantiable rejects it.","commonSituations":"`new` on base types like `Int`/`String`-constrained aliases, on abstract base classes meant for inheritance only, or on module types after a refactor.","solutions":["Replace the type with a concrete, non-abstract class that can be instantiated.","If the type is a union/alias, instantiate one of its concrete members instead.","Use `amend` semantics (`parent { ... }`) rather than `new` when extending an existing object."],"exampleFix":"// before\nnew AbstractServer { port = 8080 } // abstract\n// after\nnew HttpServer {\n  port = 8080\n}","handlingStrategy":"validation","validationCode":"// Pkl\n// ensure the type is a concrete class before `new`\n// new ConcreteClass { ... }  // OK; new AbstractThing { ... } fails","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Check that the type after `new` is a concrete, open-for-instantiation class.","Avoid `new` on modules, unions, and abstract bases.","Use amendment (`parent { ... }`) when extending existing objects."],"tags":["pkl","instantiation","abstract-class","type-system"],"backgroundTag":"invalid-constructor-argument","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}