{"record":{"id":"3eb045843b7f64c5","repo":"apple/pkl","slug":"methodmustbeconst-3eb045","errorCode":"methodMustBeConst","errorMessage":"Cannot call method `{0}` from here because it is not `const`.","messagePattern":"Cannot call method `(.+?)` from here because it is not `const`\\.","errorType":"error_code","errorClass":"VmException","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/ast/expression/member/InvokeMethodVirtualNode.java","lineNumber":162,"sourceCode":"  }\n\n  @Override\n  public WrapperNode createWrapper(ProbeNode probe) {\n    return new InvokeMethodVirtualNodeWrapper(\n        sourceSection,\n        methodName,\n        argumentNodes,\n        lookupMode,\n        needsConst,\n        argsRequireInference,\n        this,\n        probe);\n  }\n\n  private void checkConst(ClassMethod method) {\n    if (needsConst && !method.isConst()) {\n      CompilerDirectives.transferToInterpreter();\n      throw exceptionBuilder().evalError(\"methodMustBeConst\", methodName.toString()).build();\n    }\n  }\n}\n","sourceCodeStart":144,"sourceCodeEnd":166,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/ast/expression/member/InvokeMethodVirtualNode.java#L144-L166","documentation":"After a virtual method call resolves the target ClassMethod, checkConst enforces that the method is `const` when the call site requires constant evaluation (needsConst). Non-const methods cannot run in const positions, so the error is raised with the method name.","triggerScenarios":"A virtual dispatch (`receiver.method()`) from a const-required context where the resolved ClassMethod.isConst() is false.","commonSituations":"Dynamic calls into library methods from `const` properties or constant initializers; version upgrades where a previously const method lost its `const` modifier.","solutions":["Mark the resolved method `const` in its defining class","Perform the call in a non-const context and store the result in a regular property","Substitute a constant literal or `const` property value"],"exampleFix":"// before\nconst c = obj.compute() // compute not const\n// after\n// in obj's class:\nconst function compute() = 1","handlingStrategy":"validation","validationCode":"// only call methods marked `const` in const contexts:\n// const function compute() = 1","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Check the `const` modifier on dynamically dispatched methods used in constant properties","Avoid dynamic dispatch inside const expressions","Audit const call sites after library upgrades"],"tags":["pkl","const","virtual-call"],"backgroundTag":"method-not-const","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"}