apple/pkl · error

cannotFindMatchingCollectionElement

cannotFindMatchingCollectionElement

Error message

cannotFindMatchingCollectionElement

What it means

Thrown by Set.find(function) when no element of the set satisfies the predicate — the lambda returned false for every element. The library scans the set with the predicate and, finding no match, throws with the collection attached as a program value.

Solutions

  1. Use Set.firstOrNull (or check membership with Set.contains) when absence is possible
  2. Verify the predicate logic and comparisons are correct
  3. Normalize casing/format in the predicate before comparing
  4. Inspect the 'Collection' value in the error to see what the set actually contained

Example fix

// before
val found = set.find((s) -> s == name) // throws if absent
// after
val found = set.firstOrNull((s) -> s == name)
if (found == null) { /* handle missing */ }
Defensive patterns

Strategy: fallback

Validate before calling

if (set.contains(expected)) { ... } else { /* handle missing */ }

Prevention

When it happens

Trigger: Calling someSet.find((e) -> predicate(e)) where the predicate is false for all elements (e.g. find((s) -> s == "missing")).

Common situations: Searching for a value that isn't in the set, a predicate with a typo or wrong comparison, case/format mismatches in string comparisons, or data changes that removed the expected element.

Understand the failure class

Background: Record Not Found Errors: "not found", RecordNotFound, and "was not found" — what they mean and how to fix them — this error's family across 28 libraries.

Related errors


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

Appendix: source

Thrown at pkl-core/src/main/java/org/pkl/core/stdlib/base/SetNodes.java:191

  public abstract static class contains extends ExternalMethod1Node {
    @Specialization
    protected boolean eval(VmSet self, Object element) {
      return self.contains(element);
    }
  }

  public abstract static class find extends ExternalMethod1Node {
    @Child private ApplyVmFunction1Node applyLambdaNode = ApplyVmFunction1Node.create();

    @Specialization
    protected Object eval(VmSet self, VmFunction function) {
      for (var elem : self) {
        if (applyLambdaNode.executeBoolean(function, elem)) return elem;
      }

      CompilerDirectives.transferToInterpreter();
      throw exceptionBuilder()
          .evalError("cannotFindMatchingCollectionElement")
          .withProgramValue("Collection", self)
          .build();
    }
  }

  public abstract static class findOrNull extends ExternalMethod1Node {
    @Child private ApplyVmFunction1Node applyLambdaNode = ApplyVmFunction1Node.create();

    @Specialization
    protected Object eval(VmSet self, VmFunction function) {
      for (var elem : self) {
        if (applyLambdaNode.executeBoolean(function, elem)) return elem;
      }
      return VmNull.withoutDefault();
    }
  }

View on GitHub (pinned to f3efcbfc9b)