{"record":{"id":"6197bd9afbfd9c5f","repo":"apple/pkl","slug":"cannotinstantiateabstractclass","errorCode":"cannotInstantiateAbstractClass","errorMessage":"cannotInstantiateAbstractClass","messagePattern":"cannotInstantiateAbstractClass","errorType":"error_code","errorClass":"VmException","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/runtime/VmUtils.java","lineNumber":897,"sourceCode":"              // another function besides type checking, namely setting frame slot\n              new TypeNode.UnknownTypeNode(VmUtils.unavailableSourceSection())\n              : unresolvedNode.execute(frame);\n\n      descriptor.setSlotKind(i, typeNode.getFrameSlotKind());\n      typeNode.initWriteSlotNode(i);\n      resolvedNodes[i] = typeNode;\n    }\n\n    return resolvedNodes;\n  }\n\n  public static void checkIsInstantiable(VmClass parentClass, @Nullable Node parentNode) {\n    if (parentClass.isInstantiable()) return;\n\n    CompilerDirectives.transferToInterpreter();\n\n    if (parentClass.isAbstract()) {\n      throw new VmExceptionBuilder()\n          .evalError(\"cannotInstantiateAbstractClass\", parentClass)\n          .withOptionalLocation(parentNode)\n          .build();\n    }\n\n    assert parentClass.isExternal();\n    throw new VmExceptionBuilder()\n        .evalError(\"cannotInstantiateExternalClass\", parentClass)\n        .withOptionalLocation(parentNode)\n        .build();\n  }\n\n  @TruffleBoundary\n  public static Pattern compilePattern(String pattern, Node location) {\n    try {\n      return Pattern.compile(pattern, Pattern.UNICODE_CHARACTER_CLASS | Pattern.UNICODE_CASE);\n    } catch (PatternSyntaxException e) {\n      throw new VmExceptionBuilder()","sourceCodeStart":879,"sourceCodeEnd":915,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/runtime/VmUtils.java#L879-L915","documentation":"VmUtils.checkIsInstantiable throws 'cannotInstantiateAbstractClass' when code attempts to instantiate a class that is not instantiable (abstract classes, and classes such as base module abstract types). The input at fault is a VmClass with isInstantiable()==false used in an instantiation/new-expression context.","triggerScenarios":"`new SomeAbstractClass {}` (or a host call instantiating it) where `parentClass.isAbstract()` is true; typically the user picks a base module/class instead of a concrete subclass.","commonSituations":"Instantiating a base 'template' class intended only for extension (common in Am tooling and module hierarchies), refactoring that marked a class abstract after subclassing.","solutions":["Instantiate a concrete subclass instead of the abstract class.","If the intent is a prototype/type reference, use the class itself rather than an instance."],"exampleFix":"// before\nval svc = new Service {} // Service is abstract\n// after\nval svc = new HttpService {} // concrete subclass","handlingStrategy":"validation","validationCode":"// at authoring time: only instantiate concrete classes\n// if (Class.isAbstract(cls)) throw new Error(cls.name + \" is abstract\")","typeGuard":"function isInstantiable(cls) { return !cls.isAbstract && !cls.isExternal; }","tryCatchPattern":"try { new Cls {} } catch (e) { /* cannotInstantiateAbstractClass: choose a subclass */ }","preventionTips":["Document which classes are abstract base types","Name abstract bases clearly (e.g. AbstractService, BaseService)","Rely on typed bindings so abstract-ness is caught at build time"],"tags":["pkl","class","instantiation","type-system"],"backgroundTag":"unsupported-operation","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}