{"record":{"id":"7ef74d912114197e","repo":"apple/pkl","slug":"methodmustbeconst","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/InvokeLexicalClassMethodNode.java","lineNumber":44,"sourceCode":" * abstract, and is lexically scoped).\n */\npublic final class InvokeLexicalClassMethodNode extends AbstractInvokeLexicalMethodNode {\n  public InvokeLexicalClassMethodNode(\n      SourceSection sourceSection,\n      Identifier methodName,\n      int levelsUp,\n      ExpressionNode[] argumentNodes,\n      boolean needsConst,\n      boolean argsRequireInference) {\n    super(sourceSection, methodName, levelsUp, argumentNodes, needsConst, 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":26,"sourceCodeEnd":55,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/ast/expression/member/InvokeLexicalClassMethodNode.java#L26-L55","documentation":"Pkl requires that methods invoked in `const` contexts (e.g. inside `const` property values or module constant positions) be declared `const`. InvokeLexicalClassMethodNode checks the lexically resolved class method and throws this error if `method.isConst()` is false. Const expressions must be evaluable without arbitrary side effects.","triggerScenarios":"Calling a class method via a lexical (implicit receiver) reference from a position that requires const-ness (doCheckConst), where the method is not declared `const`.","commonSituations":"Using a helper function in a `const` property, default value, or annotation-style position; library method not marked `const` while callers expect constant evaluation.","solutions":["Declare the method `const`: `function foo(): Int = ...` becomes `const function foo(): Int = ...` if it qualifies","Inline a constant expression instead of calling the method","Remove the call from the const-required position"],"exampleFix":"// before\nclass Config {\n  function size(): Int = 3\n  x: Int = size() // used in const context\n}\n// after\nclass Config {\n  const function size(): Int = 3\n  x: Int = size()\n}","handlingStrategy":"validation","validationCode":"// check the method is declared const before calling in const context:\n// const function size(): Int = 3","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Mark pure helper functions `const` by default","Reserve non-const methods for effectful or dynamic computations","Run `pkl` analysis/IDE inspections to catch const violations early"],"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"}