apple/pkl · error · VmException
objectCannotHaveEntry
objectCannotHaveEntry
Error message
Object of type `{0}` cannot have an entry. What it means
Pkl throws `objectCannotHaveEntry` when an entry (`[key] = value`) appears in an object that cannot hold entries. In elementsEntriesFallback, a typed object class parent, or a Mapping when the literal is elements-only (no entries found and isElementsOnly), triggers this error instead of the element variant.
Solutions
- Remove the entry from the typed object literal.
- Convert the entry to a named property (`key = value`) if the class declares it.
- Change the parent type to Mapping/Dynamic if key-value members are required.
Example fix
// before
new Server { // typed class
["port"] = 8080 // entry not allowed
}
// after
new Server {
port = 8080
} Defensive patterns
Strategy: type-guard
Validate before calling
// Pkl
if (value is TypedObject) {
throw "entries are not allowed on typed objects; use properties"
} Prevention
- Translate Dynamic-style `[key] = value` members to named properties when introducing a class type.
- Use Mapping only when key-value members are genuinely required.
- Run pkl eval/tests to catch shape mismatches early.
When it happens
Trigger: Writing `["key"] = value` inside a literal whose parent is a typed object class, or a literal detected as elements-only where entries are disallowed by the parent type.
Common situations: Adding key/value entries to an object declared with a class type; migrating a Dynamic to a typed class without converting entries to properties.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- objectCannotHaveElement
- cannotFindPropertyInObject
- cannotRenderObjectWithElementsAndOtherMembers
- abstractMemberCannotHaveBody
- abstractMethodInNonAbstractType
AI-assisted analysis of apple/pkl@f3efcbfc9b (2026-09-08).
Data as JSON: /api/errors/b4eb8bd0e79adaee.
Report an issue: GitHub.
Appendix: source
Thrown at pkl-core/src/main/java/org/pkl/core/ast/expression/literal/SpecializedObjectLiteralNode.java:307
var parentIsClass = parent instanceof VmClass;
var parentClass = parentIsClass ? (VmClass) parent : VmUtils.getClass(parent);
VmUtils.checkIsInstantiable(parentClass, getParentNode());
var property = findFirstNonDefaultProperty(members);
if (property != null
&& (parentClass == BaseModule.getListingClass()
|| parentClass == BaseModule.getMappingClass())) {
throw exceptionBuilder()
.evalError("objectCannotHaveProperty", parentClass)
.withSourceSection(property.getHeaderSection())
.build();
}
assert firstElemOrEntry != null;
if (isTypedObjectClass(parentClass)
|| (isElementsOnly && parentClass == BaseModule.getMappingClass())) {
throw exceptionBuilder()
.evalError(
isElementsOnly ? "objectCannotHaveElement" : "objectCannotHaveEntry", parentClass)
.withSourceSection(firstElemOrEntry.getHeaderSection())
.build();
}
throw exceptionBuilder().unreachableCode().build();
}
}
View on GitHub (pinned to f3efcbfc9b)