apple/pkl · error · VmException
cannotRenderObjectWithElementsAndOtherMembers
cannotRenderObjectWithElementsAndOtherMembers
Error message
cannotRenderObjectWithElementsAndOtherMembers
What it means
Renderers (e.g. YAML/JSON property output in stdlib) cannot serialize an object that contains BOTH elements (list-style members) and properties/entries. When visitDynamic on a rendered object finds element members alongside other members, it throws this error because the target format has no representation for such a hybrid object. The object must be reshaped so it is purely map-like or purely list-like.
Source
Thrown at pkl-core/src/main/java/org/pkl/core/stdlib/AbstractRenderer.java:247
} else if (member.isEntry()) {
if (!canRenderPropertyOrEntry) cannotRenderObjectWithElementsAndOtherMembers(value);
doVisitEntry(memberKey, memberValue, sourceSection, isFirst);
} else {
doVisitElement((long) memberKey, memberValue, sourceSection, isFirst.getAndSetFalse());
}
return true;
});
enclosingValue = prevEnclosingValue;
endDynamic(value, isFirst.get());
}
protected boolean canRenderPropertyOrEntryOf(VmDynamic object) {
return !object.hasElements();
}
private void cannotRenderObjectWithElementsAndOtherMembers(VmDynamic object) {
throw new VmExceptionBuilder()
.evalError("cannotRenderObjectWithElementsAndOtherMembers", name)
.withProgramValue("Object", object)
.build();
}
@Override
public final void visitListing(VmListing value) {
value.force(false, false);
startListing(value);
var prevEnclosingValue = enclosingValue;
enclosingValue = value;
var isFirst = new MutableBoolean(true);
value.iterateAlreadyForcedMemberValues(
(memberKey, member, memberValue) -> {
assert member.isElement();
doVisitElement(View on GitHub (pinned to f3efcbfc9b)
Solutions
- Restructure the object so elements and properties/entries are not mixed: put elements in a separate Listing-valued property.
- If the intent is a list, use `new Listing { ... }` instead of a Dynamic with elements.
- If the intent is a map/object, convert the elements to entries or properties with distinct keys.
- Convert to a Map or Listing via toMap()/toList() before rendering.
Example fix
// before
output {
renderer = new YamlRenderer {}
value = new Dynamic {
"elem1"
"elem2"
name = "x"
}
}
// after
output {
renderer = new YamlRenderer {}
value = new Dynamic {
items = new Listing { "elem1"; "elem2" }
name = "x"
}
} Defensive patterns
Strategy: validation
Validate before calling
// guard before rendering function isHomogeneous(o) = o.isEmpty || (o.hasElements && o.hasOnlyElements()) || (!o.hasElements)
Try / catch
try { render(value) } catch (e) { if (e.message.contains('cannotRenderObjectWithElementsAndOtherMembers')) render(restructure(value)) else throw e } Prevention
- Keep rendered objects purely map-like or purely list-like
- Use Listing for sequences and Dynamic/Map for named fields
- Add a pre-render sanity test over representative output values
When it happens
Trigger: Calling a renderer (e.g. YamlRenderer/XmlRenderer render) on a Dynamic object created with a mix of element entries (`foo { "a"; "b" }` style elements) and properties or entries in the same object.
Common situations: Building config output where an object mixes array items and named fields; accidentally adding elements to an object intended as a map; rendering amending objects that inherit elements plus properties.
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
- cannotRenderNonStringMap
- cannotRenderNonScalarMap
- Values of type `Duration` cannot be rendered as JSON. Value:
- Values of type `DataSize` cannot be rendered as JSON. Value:
- Values of type `Bytes` cannot be rendered as JSON. Value: %s
AI-assisted analysis of apple/pkl@f3efcbfc9b (2026-09-08).
Data as JSON: /api/errors/6c740d013fc2c822.
Report an issue: GitHub.