{"record":{"id":"5bf8628c4405a851","repo":"apple/pkl","slug":"methodmustbeconst-5bf862","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/InvokeQualifiedClassMethodNode.java","lineNumber":47,"sourceCode":"      ExpressionNode[] argumentNodes,\n      boolean needsConst,\n      ExpressionNode getReceiverNode,\n      boolean argsRequireInference) {\n    super(\n        sourceSection,\n        methodName,\n        argumentNodes,\n        needsConst,\n        getReceiverNode,\n        argsRequireInference);\n  }\n\n  @Override\n  protected void doCheckConst(VmObjectLike owner) {\n    var method = owner.getVmClass().getDeclaredMethod(methodName);\n    assert method != null;\n    if (!method.isConst()) {\n      throw exceptionBuilder().evalError(\"methodMustBeConst\", methodName).build();\n    }\n  }\n\n  @Override\n  protected Method getMethod(VmObjectLike owner) {\n    var method = owner.getVmClass().getDeclaredMethod(methodName);\n    assert method != null;\n    return method;\n  }\n}\n","sourceCodeStart":29,"sourceCodeEnd":58,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/ast/expression/member/InvokeQualifiedClassMethodNode.java#L29-L58","documentation":"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.","triggerScenarios":"A qualified call to a class method from within a const-required evaluation context where getDeclaredMethod(methodName).isConst() is false.","commonSituations":"Calling `SomeClass.someHelper()` to build a constant; helper defined without `const` in a base class or library.","solutions":["Add the `const` modifier to the class method definition","Use a const property instead of calling the method","Move the call out of the const context"],"exampleFix":"// before\nclass A { function f() = 1 }\nconst v = A.f()\n// after\nclass A { const function f() = 1 }\nconst v = A.f()","handlingStrategy":"validation","validationCode":"// ensure qualified class methods used in const contexts are declared:\n// const function f() = 1","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Declare qualified helper methods `const`","Keep constant construction to literals and const properties","Verify class APIs expose const variants when needed for constants"],"tags":["pkl","const","method-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"}