{"record":{"id":"4b50b887b53d4cc2","repo":"apple/pkl","slug":"cannotspreadobject","errorCode":"cannotSpreadObject","errorMessage":"Cannot spread value of type `{0}` into object of type `{1}`.","messagePattern":"Cannot spread value of type `(.+?)` into object of type `(.+?)`\\.","errorType":"exception","errorClass":"VmException","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/ast/expression/generator/GeneratorSpreadNode.java","lineNumber":242,"sourceCode":"\n  private void doEvalTyped(VirtualFrame frame, VmClass clazz, ObjectData data, VmObject iterable) {\n    iterable.forceAndIterateMemberValues(\n        (key, member, value) -> {\n          if (member.isElement() || member.isEntry()) {\n            cannotHaveMember(clazz, member);\n          }\n          checkIsValidTypedProperty(clazz, member);\n          data.addProperty(frame, createMember(member, value), this);\n          return true;\n        });\n  }\n\n  // handles both `List` and `Set`\n  private void doEvalCollection(\n      VirtualFrame frame, VmClass parent, ObjectData data, VmCollection iterable) {\n    if (isTypedObjectClass(parent) || parent == getMappingClass()) {\n      CompilerDirectives.transferToInterpreter();\n      throw exceptionBuilder()\n          .evalError(\"cannotSpreadObject\", iterable.getVmClass(), parent)\n          .withHint(\n              \"`List` and `Set` can only be spread into objects of type `Dynamic` and `Listing`.\")\n          .withProgramValue(\"Value\", iterable)\n          .build();\n    }\n    spreadIterable(frame, data, iterable);\n  }\n\n  private void doEvalMap(VirtualFrame frame, VmClass parent, ObjectData data, VmMap iterable) {\n    if (isTypedObjectClass(parent) || parent == getListingClass()) {\n      CompilerDirectives.transferToInterpreter();\n      throw exceptionBuilder()\n          .evalError(\"cannotSpreadObject\", iterable.getVmClass(), parent)\n          .withHint(\"`Map` can only be spread into objects of type `Dynamic` and `Mapping`.\")\n          .withProgramValue(\"Value\", iterable)\n          .build();\n    }","sourceCodeStart":224,"sourceCodeEnd":260,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/ast/expression/generator/GeneratorSpreadNode.java#L224-L260","documentation":"A `List` or `Set` was spread with `...` into a typed object or a `Mapping`. List/Set spreads add elements, which only `Dynamic` and `Listing` objects accept. Pkl rejects the spread at evaluation time.","triggerScenarios":"`...someList` or `...someSet` inside a `new Foo { ... }` (typed object) body, or inside a `Mapping { ... }` body.","commonSituations":"Spreading default element lists into a typed schema object or a Mapping, e.g. `server { ...commonPorts }` where `server` is a typed class or Mapping.","solutions":["Spread the List/Set into a `Dynamic` or `Listing` object instead.","Convert the collection to a Map and spread into a `Mapping`: `...Map.from(list)` is not valid; instead use element syntax appropriate for the target type.","Remove the spread and assign properties/elements explicitly."],"exampleFix":"// before\nnew Server {\n  ...[8080, 9090] // List into typed object\n}\n// after\nnew Listing<Int> {\n  ...[8080, 9090]\n}","handlingStrategy":"validation","validationCode":"// ensure target is Dynamic/Listing before spreading List/Set\nassert(isDynamic(target) || target is Listing)","typeGuard":"function canSpreadElements(target) { return target is Dynamic || target is Listing; }","tryCatchPattern":null,"preventionTips":["Remember: element spreads (List/Set/IntSeq/Bytes) go to Dynamic/Listing only.","Check the object header (new Foo {}) before adding spreads.","Convert collections with .toMap()/explicit loops when target is a Mapping."],"tags":["pkl","spread-operator","type-mismatch"],"backgroundTag":"incompatible-source-type","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"}