apple/pkl · error

operatorNotDefined2

operatorNotDefined2

Error message

operatorNotDefined2 ${op} ${leftClass} ${rightClass} (VmReference)

What it means

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.

Source

Thrown at pkl-core/src/main/java/org/pkl/core/ast/expression/binary/SubscriptNode.java:125

  private @Nullable String getReferenceHint(
      VmReference reference, VmReferenceAccessError err, Object key) {
    var myType = reference.getReferentType();
    if (err.getErrorType() != VmReferenceAccessErrorType.CANNOT_FIND_MEMBER) {
      return null;
    }
    return err.getType().equals(myType)
        ? ErrorMessages.create("operatorNotDefined2", getShortName(), myType, VmUtils.getClass(key))
        : ErrorMessages.create(
            "operatorNotDefined3", getShortName(), myType, err.getType(), VmUtils.getClass(key));
  }

  @Specialization
  protected VmReference eval(VmReference reference, Object key) {
    try {
      return reference.withSubscriptAccess(key);
    } catch (VmReferenceAccessError err) {
      CompilerDirectives.transferToInterpreter();
      throw exceptionBuilder()
          .evalError(
              "operatorNotDefined2", getShortName(), reference.exportType(), VmUtils.getClass(key))
          .withProgramValue("Left operand", reference)
          .withProgramValue("Right operand", key)
          .withHint(getReferenceHint(reference, err, key))
          .build();
    }
  }

  @Specialization
  protected long eval(VmBytes receiver, long index) {
    if (index < 0 || index >= receiver.getLength()) {
      CompilerDirectives.transferToInterpreter();
      throw exceptionBuilder()
          .evalError("elementIndexOutOfRange", index, 0, receiver.getLength() - 1)
          .withProgramValue("Value", receiver)
          .build();
    }

View on GitHub (pinned to f3efcbfc9b)

Solutions

  1. Use dot member access (`.name`) instead of subscript when accessing a fixed property
  2. Check the referenced object's type and define a matching subscript operator if dynamic access is intended
  3. Ensure the key type matches the subscript operator's declared parameter type
  4. Inspect the 'Left operand' and 'Right operand' program values in the error output to see the actual types

Example fix

// before
name = person["name"]
// after
name = person.name
Defensive patterns

Strategy: type-guard

Validate before calling

// pkl
if (ref is ClassWithSubscript) {
  value = ref[key]
} else {
  value = ref.getProperty(key)
}

Type guard

function isSubscriptable(v) { return v != null && (typeof v === 'object') && ('length' in v || Symbol.subscriptSupport in v) }

Try / catch

// catch PklException and inspect errorCode 'operatorNotDefined2'; fall back to member access

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of apple/pkl@f3efcbfc9b (2026-09-08). Data as JSON: /api/errors/d3849ff3f3447f6e. Report an issue: GitHub.