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

  1. Restructure the object so elements and properties/entries are not mixed: put elements in a separate Listing-valued property.
  2. If the intent is a list, use `new Listing { ... }` instead of a Dynamic with elements.
  3. If the intent is a map/object, convert the elements to entries or properties with distinct keys.
  4. 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

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


AI-assisted analysis of apple/pkl@f3efcbfc9b (2026-09-08). Data as JSON: /api/errors/6c740d013fc2c822. Report an issue: GitHub.