{"record":{"id":"ae3533514ebf1ff2","repo":"apple/pkl","slug":"methodmustbeconst-ae3533","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/InvokeLexicalObjectMethodNode.java","lineNumber":43,"sourceCode":"\n/** A non-virtual call of an object method that is lexically scoped. */\npublic final class InvokeLexicalObjectMethodNode extends AbstractInvokeLexicalMethodNode {\n  public InvokeLexicalObjectMethodNode(\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 member = owner.getMember(methodName);\n    assert member != null;\n    if (!VmModifier.isConst(member.getModifiers())) {\n      throw exceptionBuilder().evalError(\"methodMustBeConst\", methodName).build();\n    }\n  }\n\n  @Override\n  protected Method getMethod(VmObjectLike owner) {\n    var member = owner.getMember(methodName);\n    assert member != null && member.isLocal();\n    var method = (ObjectMethodNode) member.getMemberNode();\n    assert method != null;\n    return method;\n  }\n}\n","sourceCodeStart":25,"sourceCodeEnd":56,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/ast/expression/member/InvokeLexicalObjectMethodNode.java#L25-L56","documentation":"Same const-ness rule as class methods but for object (definition) methods: when the call site requires a constant value, InvokeLexicalObjectMethodNode looks up the member on the owner and throws if its modifiers lack the `const` flag.","triggerScenarios":"An object method invoked via lexical scope from a const-requiring position; doCheckConst finds the member and VmModifier.isConst(modifiers) is false.","commonSituations":"Calling a non-const object method while computing a const property or module-level constant; methods copied from non-const examples into const contexts.","solutions":["Mark the object method `const` in its definition","Replace the call with a `const` property or literal","Reconsider whether the call site truly needs const and relax it"],"exampleFix":"// before\nx {\n  function helper() = 42\n}\nconstY = x.helper() // const context\n// after\nx {\n  const function helper() = 42\n}","handlingStrategy":"validation","validationCode":"// declare object methods const when they should be usable in const positions:\n// const function helper() = 42","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Prefer `const` for all pure object methods","Keep const call sites limited to const methods and literals","Review library APIs for const modifiers before using them in constant properties"],"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"}