{"record":{"id":"d3849ff3f3447f6e","repo":"apple/pkl","slug":"operatornotdefined2-d3849f","errorCode":"operatorNotDefined2","errorMessage":"operatorNotDefined2 ${op} ${leftClass} ${rightClass} (VmReference)","messagePattern":"operatorNotDefined2 (.+?) (.+?) (.+?) \\(VmReference\\)","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/ast/expression/binary/SubscriptNode.java","lineNumber":125,"sourceCode":"  private @Nullable String getReferenceHint(\n      VmReference reference, VmReferenceAccessError err, Object key) {\n    var myType = reference.getReferentType();\n    if (err.getErrorType() != VmReferenceAccessErrorType.CANNOT_FIND_MEMBER) {\n      return null;\n    }\n    return err.getType().equals(myType)\n        ? ErrorMessages.create(\"operatorNotDefined2\", getShortName(), myType, VmUtils.getClass(key))\n        : ErrorMessages.create(\n            \"operatorNotDefined3\", getShortName(), myType, err.getType(), VmUtils.getClass(key));\n  }\n\n  @Specialization\n  protected VmReference eval(VmReference reference, Object key) {\n    try {\n      return reference.withSubscriptAccess(key);\n    } catch (VmReferenceAccessError err) {\n      CompilerDirectives.transferToInterpreter();\n      throw exceptionBuilder()\n          .evalError(\n              \"operatorNotDefined2\", getShortName(), reference.exportType(), VmUtils.getClass(key))\n          .withProgramValue(\"Left operand\", reference)\n          .withProgramValue(\"Right operand\", key)\n          .withHint(getReferenceHint(reference, err, key))\n          .build();\n    }\n  }\n\n  @Specialization\n  protected long eval(VmBytes receiver, long index) {\n    if (index < 0 || index >= receiver.getLength()) {\n      CompilerDirectives.transferToInterpreter();\n      throw exceptionBuilder()\n          .evalError(\"elementIndexOutOfRange\", index, 0, receiver.getLength() - 1)\n          .withProgramValue(\"Value\", receiver)\n          .build();\n    }","sourceCodeStart":107,"sourceCodeEnd":143,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/ast/expression/binary/SubscriptNode.java#L107-L143","documentation":"Pkl throws this when a subscript operator (`[key]`) is applied to a value whose type does not define that operator for the given key type. In this case the left operand was a `VmReference` (a lazily-resolved object reference) whose subscript access to the given key failed because the referenced object/type has no matching subscript operator or member. The message names the operator, and the classes of the left operand and key.","triggerScenarios":"Evaluating `someRef[someKey]` in Pkl where `someRef` is a typed object reference and the key does not match any defined subscript operator/member on the referenced object's type (e.g. string subscript on a class without `subscript(key: String)` definitions).","commonSituations":"Mistyping a member access as bracket syntax (e.g. `person[\"name\"]` instead of `person.name` on a fixed-shape object), indexing a typed object with a string when only integer listing subscripts exist, or referencing an amended object whose type erased the expected operator.","solutions":["Use dot member access (`.name`) instead of subscript when accessing a fixed property","Check the referenced object's type and define a matching subscript operator if dynamic access is intended","Ensure the key type matches the subscript operator's declared parameter type","Inspect the 'Left operand' and 'Right operand' program values in the error output to see the actual types"],"exampleFix":"// before\nname = person[\"name\"]\n// after\nname = person.name","handlingStrategy":"type-guard","validationCode":"// pkl\nif (ref is ClassWithSubscript) {\n  value = ref[key]\n} else {\n  value = ref.getProperty(key)\n}","typeGuard":"function isSubscriptable(v) { return v != null && (typeof v === 'object') && ('length' in v || Symbol.subscriptSupport in v) }","tryCatchPattern":"// catch PklException and inspect errorCode 'operatorNotDefined2'; fall back to member access","preventionTips":["Prefer dot member access for fixed-shape objects","Only use subscript operators on types that define one","Match key type to the operator's parameter type"],"tags":["pkl","subscript-operator","type-mismatch"],"backgroundTag":"unsupported-operation","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}