{"record":{"id":"b213d5a15dc43b89","repo":"apple/pkl","slug":"cannotinstantiateexternalclass","errorCode":"cannotInstantiateExternalClass","errorMessage":"cannotInstantiateExternalClass","messagePattern":"cannotInstantiateExternalClass","errorType":"error_code","errorClass":"VmException","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/runtime/VmUtils.java","lineNumber":904,"sourceCode":"    }\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()\n          .withLocation(location)\n          .evalError(\"invalidRegexSyntax\", pattern, e.getMessage())\n          .build();\n    }\n  }\n\n  @TruffleBoundary","sourceCodeStart":886,"sourceCodeEnd":922,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/runtime/VmUtils.java#L886-L922","documentation":"The external-class counterpart in `VmUtils.checkIsInstantiable`: when the class is not abstract but `isExternal()` (implemented in Java/native code, e.g. stdlib types like Int, List), it cannot be instantiated from Pkl with `new`, so cannotInstantiateExternalClass is thrown.","triggerScenarios":"`new Int {}`, `new List {}`, or `new <any external class> {}` — attempting to construct a built-in/external class from Pkl source.","commonSituations":"Confusing type names with constructors, trying to build stdlib-like values via `new` instead of literals or stdlib factory functions.","solutions":["Use literals or stdlib constructors instead: `42`, `List(1,2,3)`, `\"text\"`, `Map(...)`, etc.","Use the corresponding factory function from `pkl.*` modules rather than `new`.","Remove the `new` expression entirely if the value should just be typed, not constructed.","Define your own (non-external) class if you need a custom instance shape."],"exampleFix":"// before\nval xs = new List {}\n// after\nval xs = List()","handlingStrategy":"validation","validationCode":"// do not `new` stdlib/external types; use literals and factories\nconst xs = List(1, 2, 3);","typeGuard":"function isPklConstructible(cls) { return !cls.isExternal && !cls.isAbstract; }","tryCatchPattern":"try { new ExternalCls {} } catch (e) { /* fall back to literal/factory construction */ }","preventionTips":["Use literals and stdlib factory functions for built-in types","Never use `new` for pkl.base types","Check imports: you may want a value type, not a constructor"],"tags":["pkl","class","instantiation","stdlib"],"backgroundTag":"unsupported-operation","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"}