apple/pkl · error · VmException

cannotInstantiateType

cannotInstantiateType

Error message

Cannot instantiate type `{0}`.

What it means

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.

Solutions

  1. Replace the type with a concrete, non-abstract class that can be instantiated.
  2. If the type is a union/alias, instantiate one of its concrete members instead.
  3. Use `amend` semantics (`parent { ... }`) rather than `new` when extending an existing object.

Example fix

// before
new AbstractServer { port = 8080 } // abstract
// after
new HttpServer {
  port = 8080
}
Defensive patterns

Strategy: validation

Validate before calling

// Pkl
// ensure the type is a concrete class before `new`
// new ConcreteClass { ... }  // OK; new AbstractThing { ... } fails

Prevention

When it happens

Trigger: `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.

Common situations: `new` on base types like `Int`/`String`-constrained aliases, on abstract base classes meant for inheritance only, or on module types after a refactor.

Related errors


AI-assisted analysis of apple/pkl@f3efcbfc9b (2026-09-08). Data as JSON: /api/errors/37c694c12fe48b23. Report an issue: GitHub.

Appendix: source

Thrown at pkl-core/src/main/java/org/pkl/core/ast/expression/member/AbstractInferParentNode.java:65

    var defaultValue = typeNode.createDefaultValue(frame, language, headerSection, qualifiedName);
    if (defaultValue != null) {
      return defaultValue;
    }

    CompilerDirectives.transferToInterpreter();

    if (typeNode instanceof TypeVariableNode) {
      throw exceptionBuilder().evalError("cannotInferParent").build();
    }

    // try to produce a more specific error message than "cannotInstantiateType"
    var clazz = typeNode.getVmClass();
    if (clazz != null) {
      VmUtils.checkIsInstantiable(clazz, typeNode);
    }

    throw exceptionBuilder()
        .evalError("cannotInstantiateType", typeNode.getSourceSection().getCharacters())
        .build();
  }
}

View on GitHub (pinned to f3efcbfc9b)