{"record":{"id":"d05cdeadfc271f5b","repo":"apple/pkl","slug":"cannotflattencollectionwithnoncollectionelement","errorCode":"cannotFlattenCollectionWithNonCollectionElement","errorMessage":"cannotFlattenCollectionWithNonCollectionElement","messagePattern":"cannotFlattenCollectionWithNonCollectionElement","errorType":"error_code","errorClass":"VmException","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/runtime/VmCollection.java","lineNumber":143,"sourceCode":"        Xform.of(this).take(start).concat(replacement).concat(Xform.of(this).drop(exclusiveEnd));\n    return VmList.create(result);\n  }\n\n  @TruffleBoundary\n  public final VmCollection flatten() {\n    var builder = builder();\n    for (var elem : this) {\n      if (elem instanceof Iterable<?> iterable) {\n        builder.addAll(iterable);\n      } else if (elem instanceof VmListing listing) {\n        listing.forceAndIterateMemberValues(\n            (key, member, value) -> {\n              builder.add(value);\n              return true;\n            });\n      } else {\n        CompilerDirectives.transferToInterpreter();\n        throw new VmExceptionBuilder()\n            .evalError(\"cannotFlattenCollectionWithNonCollectionElement\")\n            .withProgramValue(\"Element\", elem)\n            .build();\n      }\n    }\n    return builder.build();\n  }\n\n  @TruffleBoundary\n  public final VmCollection zip(VmCollection other) {\n    var builder = builder();\n    var iter1 = iterator();\n    var iter2 = other.iterator();\n    while (iter1.hasNext() && iter2.hasNext()) {\n      builder.add(new VmPair(iter1.next(), iter2.next()));\n    }\n    return builder.build();\n  }","sourceCodeStart":125,"sourceCodeEnd":161,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/runtime/VmCollection.java#L125-L161","documentation":"Thrown by VmCollection.flatten() when flattening encounters an element that is itself not a collection/listing. Flattening requires every element to be a collection so it can be spliced into the result; a scalar element cannot be flattened.","triggerScenarios":"Calling collection.flatten() where at least one element is a non-collection value (e.g. an Int or String instead of a List/ListSet/Mapping). The offending element is attached as 'Element'.","commonSituations":"A list built conditionally where one branch appends a scalar instead of a nested list; data from external files where some entries are scalars.","solutions":["Check the element reported under 'Element' and wrap it in a list (e.g. [elem]) or remove it","Ensure all elements of the collection being flattened are List/ListSet/Mapping values","Use a conditional/when branch to normalize scalar entries into single-element lists before flattening"],"exampleFix":"// before\nflat = [[1,2], 3, [4]].flatten()\n// after\nflat = [[1,2], [3], [4]].flatten()","handlingStrategy":"validation","validationCode":"const flattenable = xs.every(x => typeof x === 'object' && Array.isArray(x)); if (!flattenable) throw new TypeError('flatten requires all elements to be collections')","typeGuard":"const isNested = (xs) => Array.isArray(xs) && xs.every(Array.isArray);","tryCatchPattern":null,"preventionTips":["Ensure every element is a collection; wrap scalars in a single-element list","Normalize data shapes before flattening","Test flatten on representative data including scalar branches"],"tags":["pkl","collection","flatten","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}