{"record":{"id":"75d06918a58e8eab","repo":"apple/pkl","slug":"type-mismatch-value-is-not-of-type-collection","errorCode":null,"errorMessage":"type mismatch: value is not of type Collection","messagePattern":"type mismatch: value is not of type Collection","errorType":"exception","errorClass":"VmTypeMismatchException","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/ast/type/TypeNode.java","lineNumber":1228,"sourceCode":"  }\n\n  public static final class CollectionTypeNode extends ObjectSlotTypeNode {\n    @Child private TypeNode elementTypeNode;\n\n    public CollectionTypeNode(SourceSection sourceSection, TypeNode elementTypeNode) {\n      super(sourceSection);\n      this.elementTypeNode = elementTypeNode;\n    }\n\n    @Override\n    protected Object executeLazily(VirtualFrame frame, Object value) {\n      if (value instanceof VmList vmList) {\n        return evalList(frame, vmList);\n      }\n      if (value instanceof VmSet vmSet) {\n        return evalSet(frame, vmSet);\n      }\n      throw typeMismatch(value, BaseModule.getCollectionClass());\n    }\n\n    @Override\n    public Object executeEagerly(VirtualFrame frame, Object value) {\n      if (value instanceof VmList vmList) {\n        return evalListEagerly(frame, vmList);\n      }\n      if (value instanceof VmSet vmSet) {\n        // sets are always checked eagerly\n        return evalSet(frame, vmSet);\n      }\n      throw typeMismatch(value, BaseModule.getCollectionClass());\n    }\n\n    @Override\n    public Object createDefaultValue(\n        VirtualFrame frame,\n        VmLanguage language,","sourceCodeStart":1210,"sourceCodeEnd":1246,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/ast/type/TypeNode.java#L1210-L1246","documentation":"Pkl `Collection` type check failure. The `Collection` type accepts VmList and VmSet (which are then element-wise checked/conformed via evalList/evalSet); any other value (String, Int, Dynamic, Typed object, Mapping, null) fails with `typeMismatch(value, BaseModule.getCollectionClass())`.","triggerScenarios":"Assigning a non-list/non-set value to a property or parameter typed `Collection`; passing a Mapping or a single element where a Collection is required; null where the collection type has no default.","commonSituations":"Confusing `Mapping` (keyed) with `Collection` (sequence), forgetting to wrap a single item in a list (`List(...)`), converting from JSON where an object vs array distinction differs.","solutions":["Wrap the value in a list: `List(item)` or `new Listing { item }`.","If it is a Mapping, change the declared type to `Mapping` instead of `Collection`.","If any sequence kind works, declare `List` or `Set` explicitly to match what you produce.","Ensure default values are `List()` rather than null for collection-typed properties."],"exampleFix":"// before\ntags: Collection = \"red\"\n// after\ntags: Collection = List(\"red\")","handlingStrategy":"type-guard","validationCode":"// Pkl\nif (value is List || value is Set) {\n  coll: Collection = value\n} else {\n  coll: Collection = List(value)\n}","typeGuard":"function isCollection(value: Any): Boolean = value is List || value is Set","tryCatchPattern":"// Guard with `is List || is Set` and wrap singles in List(...); embedders: catch PklException with 'not of type Collection'.","preventionTips":["Wrap single items with List(...) before assigning to collection-typed slots","Don't confuse Mapping with Collection","Default collection-typed properties to List() rather than null"],"tags":["pkl","type-mismatch","collection","typecheck"],"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"}