{"record":{"id":"c4ccf7e506beeda7","repo":"apple/pkl","slug":"cannotfindmatchingcollectionelement-c4ccf7","errorCode":"cannotFindMatchingCollectionElement","errorMessage":"cannotFindMatchingCollectionElement","messagePattern":"cannotFindMatchingCollectionElement","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/stdlib/base/SetNodes.java","lineNumber":191,"sourceCode":"\n  public abstract static class contains extends ExternalMethod1Node {\n    @Specialization\n    protected boolean eval(VmSet self, Object element) {\n      return self.contains(element);\n    }\n  }\n\n  public abstract static class find extends ExternalMethod1Node {\n    @Child private ApplyVmFunction1Node applyLambdaNode = ApplyVmFunction1Node.create();\n\n    @Specialization\n    protected Object eval(VmSet self, VmFunction function) {\n      for (var elem : self) {\n        if (applyLambdaNode.executeBoolean(function, elem)) return elem;\n      }\n\n      CompilerDirectives.transferToInterpreter();\n      throw exceptionBuilder()\n          .evalError(\"cannotFindMatchingCollectionElement\")\n          .withProgramValue(\"Collection\", self)\n          .build();\n    }\n  }\n\n  public abstract static class findOrNull extends ExternalMethod1Node {\n    @Child private ApplyVmFunction1Node applyLambdaNode = ApplyVmFunction1Node.create();\n\n    @Specialization\n    protected Object eval(VmSet self, VmFunction function) {\n      for (var elem : self) {\n        if (applyLambdaNode.executeBoolean(function, elem)) return elem;\n      }\n      return VmNull.withoutDefault();\n    }\n  }\n","sourceCodeStart":173,"sourceCodeEnd":209,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/stdlib/base/SetNodes.java#L173-L209","documentation":"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.","triggerScenarios":"Calling someSet.find((e) -> predicate(e)) where the predicate is false for all elements (e.g. find((s) -> s == \"missing\")).","commonSituations":"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.","solutions":["Use Set.firstOrNull (or check membership with Set.contains) when absence is possible","Verify the predicate logic and comparisons are correct","Normalize casing/format in the predicate before comparing","Inspect the 'Collection' value in the error to see what the set actually contained"],"exampleFix":"// before\nval found = set.find((s) -> s == name) // throws if absent\n// after\nval found = set.firstOrNull((s) -> s == name)\nif (found == null) { /* handle missing */ }","handlingStrategy":"fallback","validationCode":"if (set.contains(expected)) { ... } else { /* handle missing */ }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Prefer firstOrNull over find when absence is a normal outcome","Check membership with contains before searching","Test predicate logic against representative data","Normalize string case/format before comparing elements"],"tags":["pkl","set","not-found","stdlib"],"backgroundTag":"record-not-found","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}