apple/pkl · error · VmException

methodMustBeConst

methodMustBeConst

Error message

Cannot call method `{0}` from here because it is not `const`.

What it means

InvokeQualifiedClassMethodNode performs qualified calls to class methods (e.g. `ClassName.method()` / receiver-qualified). When the call site is a const position, doCheckConst verifies the declared method on the owner's VM class is const and throws otherwise.

Source

Thrown at pkl-core/src/main/java/org/pkl/core/ast/expression/member/InvokeQualifiedClassMethodNode.java:47

      ExpressionNode[] argumentNodes,
      boolean needsConst,
      ExpressionNode getReceiverNode,
      boolean argsRequireInference) {
    super(
        sourceSection,
        methodName,
        argumentNodes,
        needsConst,
        getReceiverNode,
        argsRequireInference);
  }

  @Override
  protected void doCheckConst(VmObjectLike owner) {
    var method = owner.getVmClass().getDeclaredMethod(methodName);
    assert method != null;
    if (!method.isConst()) {
      throw exceptionBuilder().evalError("methodMustBeConst", methodName).build();
    }
  }

  @Override
  protected Method getMethod(VmObjectLike owner) {
    var method = owner.getVmClass().getDeclaredMethod(methodName);
    assert method != null;
    return method;
  }
}

View on GitHub (pinned to f3efcbfc9b)

Solutions

  1. Add the `const` modifier to the class method definition
  2. Use a const property instead of calling the method
  3. Move the call out of the const context

Example fix

// before
class A { function f() = 1 }
const v = A.f()
// after
class A { const function f() = 1 }
const v = A.f()
Defensive patterns

Strategy: validation

Validate before calling

// ensure qualified class methods used in const contexts are declared:
// const function f() = 1

Prevention

When it happens

Trigger: A qualified call to a class method from within a const-required evaluation context where getDeclaredMethod(methodName).isConst() is false.

Common situations: Calling `SomeClass.someHelper()` to build a constant; helper defined without `const` in a base class or library.

Related errors


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