{"record":{"id":"0035fd08e7dcef4e","repo":"apple/pkl","slug":"cannotfindmatchingcollectionelement","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/ListNodes.java","lineNumber":238,"sourceCode":"\n  public abstract static class contains extends ExternalMethod1Node {\n    @Specialization\n    protected boolean eval(VmList 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(VmList 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(VmList 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":220,"sourceCodeEnd":256,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/stdlib/base/ListNodes.java#L220-L256","documentation":"List.find(function) returns the first element satisfying the predicate. If no element matches, Pkl throws cannotFindMatchingCollectionElement instead of returning null, because the Pkl language has no null value. The collection is attached for debugging.","triggerScenarios":"Calling List.find((e) -> ...) where the predicate returns false for every element of an empty or non-matching list.","commonSituations":"Looking up a record in a config list by key/id that is absent after data changes; filtering assumptions broken by upstream data edits; searching an empty list.","solutions":["Guard with a length/predicate pre-check: list.filter(...).isEmpty before find","Use List.firstOrNull-style alternatives via fold or filter(...).isEmpty handling in Pkl code","Restructure to use filter and handle an empty result explicitly","Fix the predicate or ensure the expected element exists in the data"],"exampleFix":"// before\nlocal entry = servers.find((s) -> s.name == \"db\")\n// after\nlocal matches = servers.filter((s) -> s.name == \"db\")\nlocal entry = matches.isEmpty ? defaultServer : matches.first","handlingStrategy":"validation","validationCode":"if (list.filter(pred).isEmpty) throw new Error(\"no element matches predicate\")","typeGuard":"function hasMatch(list, pred) { return list.some(pred); }","tryCatchPattern":null,"preventionTips":["Prefer filter + isEmpty checks over find when absence is possible","Supply defaults for optional lookups","Keep predicates in sync with the data schema"],"tags":["pkl","list","find","predicate"],"backgroundTag":"resource-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"}