apple/pkl · error

cannotInvokeAbstractMethod

cannotInvokeAbstractMethod

Error message

cannotInvokeAbstractMethod ${functionName}

What it means

This Truffle node is compiled in place of a call to an abstract (declaration-only) Pkl function. Executing it always throws: abstract functions have no body, so invoking one directly is undefined and Pkl reports cannotInvokeAbstractMethod with the function name.

Source

Thrown at pkl-core/src/main/java/org/pkl/core/ast/builder/CannotInvokeAbstractFunctionNode.java:34

package org.pkl.core.ast.builder;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.source.SourceSection;
import org.pkl.core.ast.ExpressionNode;

public final class CannotInvokeAbstractFunctionNode extends ExpressionNode {
  private final String functionName;

  public CannotInvokeAbstractFunctionNode(SourceSection section, String functionName) {
    super(section);
    this.functionName = functionName;
  }

  @Override
  public Object executeGeneric(VirtualFrame frame) {
    CompilerDirectives.transferToInterpreter();
    throw exceptionBuilder().evalError("cannotInvokeAbstractMethod", functionName).build();
  }
}

View on GitHub (pinned to f3efcbfc9b)

Solutions

  1. Provide a concrete override of the named function in the object/subclass being instantiated
  2. Do not instantiate the abstract type directly; instantiate a concrete subtype
  3. Fix override name/signature mismatch so the definition actually overrides the abstract member

Example fix

// before
abstract class Renderer { function render(): String }
renderer = new Renderer {} // no override
// after
abstract class Renderer { function render(): String }
renderer = new Renderer { function render() = "hello" }
Defensive patterns

Strategy: type-guard

Validate before calling

// pkl-side guard: only call when the receiver provides the function
// renderer is ConcreteRenderer && renderer.hasProperty("render")

Type guard

function canInvoke(obj, fn) { return obj.getClass().isConcrete && obj.hasProperty(fn) } // conceptually: ensure concrete subtype before calling

Try / catch

// Wrap invocation in try/catch for PklError matching cannotInvokeAbstractMethod, then fall back to a default implementation

Prevention

When it happens

Trigger: Calling an abstract function on an object that does not provide a concrete override — e.g. invoking `render()` on a value typed as an abstract module/class whose subtype was expected to implement it.

Common situations: Forgetting to override an abstract function in a subclass/typed object; calling methods on `new` instances of abstract types; refactoring renamed an override so it no longer matches.

Understand the failure class

Background: "NotImplementedError: Subclasses should override this method" / "must be implemented" — abstract method errors explained — this error's family across 40 libraries.

Related errors


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