{"record":{"id":"5c6ef54e1e7cf483","repo":"apple/pkl","slug":"objectcannothaveelement-5c6ef5","errorCode":"objectCannotHaveElement","errorMessage":"Object of type `{0}` cannot have an element.","messagePattern":"Object of type `(.+?)` cannot have an element\\.","errorType":"error_code","errorClass":"VmException","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/ast/expression/literal/SpecializedObjectLiteralNode.java","lineNumber":307,"sourceCode":"    var parentIsClass = parent instanceof VmClass;\n    var parentClass = parentIsClass ? (VmClass) parent : VmUtils.getClass(parent);\n    VmUtils.checkIsInstantiable(parentClass, getParentNode());\n\n    var property = findFirstNonDefaultProperty(members);\n    if (property != null\n        && (parentClass == BaseModule.getListingClass()\n            || parentClass == BaseModule.getMappingClass())) {\n      throw exceptionBuilder()\n          .evalError(\"objectCannotHaveProperty\", parentClass)\n          .withSourceSection(property.getHeaderSection())\n          .build();\n    }\n\n    assert firstElemOrEntry != null;\n\n    if (isTypedObjectClass(parentClass)\n        || (isElementsOnly && parentClass == BaseModule.getMappingClass())) {\n      throw exceptionBuilder()\n          .evalError(\n              isElementsOnly ? \"objectCannotHaveElement\" : \"objectCannotHaveEntry\", parentClass)\n          .withSourceSection(firstElemOrEntry.getHeaderSection())\n          .build();\n    }\n\n    throw exceptionBuilder().unreachableCode().build();\n  }\n}\n","sourceCodeStart":289,"sourceCodeEnd":317,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/ast/expression/literal/SpecializedObjectLiteralNode.java#L289-L317","documentation":"Pkl throws `objectCannotHaveElement` when an element expression (or any member when the literal is elements-only) appears in an object whose class cannot hold elements — typed object classes, and Mappings when the literal contains only elements. In elementsEntriesFallback, a typed parent class or an elements-only Mapping literal triggers this.","triggerScenarios":"Writing `new { ... }` element members in a literal whose parent is a typed object class, or using element syntax inside a Mapping literal that contains only elements.","commonSituations":"Adding anonymous elements to an object declared with a `: Class` type; writing `new Mapping { ... }` with bare element bodies instead of `[key] = value` entries.","solutions":["Remove the element members from the typed object literal.","For a Mapping, replace elements with `[key] = value` entries.","Use a Listing/Dynamic if untyped element members are what you intend."],"exampleFix":"// before\nnew Server { // typed class\n  new { port = 8080 } // element not allowed\n}\n// after\nnew Server {\n  port = 8080\n}","handlingStrategy":"type-guard","validationCode":"// Pkl\nif (value is Mapping) {\n  throw \"use [key] = value entries, not elements\"\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use `new Listing` or Dynamic for element-style members, not typed classes.","Convert bare element bodies into `[key] = value` entries inside Mappings.","Verify the declared type before writing literal members."],"tags":["pkl","typed-object","mapping","object-shape"],"backgroundTag":"invalid-argument-value","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"}