apple/pkl · error · VmException

typeMismatch

Error message

typeMismatch

What it means

When rendering a `Dynamic` value as an XML element, the renderer reads its `name` member and requires it to be a String (since there is no dedicated XmlElement Pkl class yet). If `name` is any other type it throws a typeMismatch against the String class.

Source

Thrown at pkl-core/src/main/java/org/pkl/core/stdlib/xml/RendererNodes.java:343

          || value instanceof Double;
    }

    private static boolean isContent(Object value) {
      return isScalar(value) || isXmlCData(value) || isXmlComment(value);
    }

    private static boolean isXmlElement(Object value) {
      return value instanceof VmDynamic dynamic
          && VmUtils.readMemberOrNull(dynamic, Identifier.IS_XML_ELEMENT) == Boolean.TRUE;
    }

    private void renderXmlElement(VmDynamic value) {
      assert isXmlElement(value);

      var name = VmUtils.readMember(value, Identifier.NAME);
      // this check will be unnecessary once we have an XmlElement Pkl class
      if (!(name instanceof String)) {
        throw new VmExceptionBuilder().typeMismatch(value, BaseModule.getStringClass()).build();
      }

      Object attributes = VmUtils.readMember(value, Identifier.ATTRIBUTES);
      // this check will be unnecessary once we have an XmlElement Pkl class
      if (!(attributes instanceof VmMapping)) {
        throw new VmExceptionBuilder().typeMismatch(value, BaseModule.getMappingClass()).build();
      }

      var isBlockFormat = VmUtils.readMember(value, Identifier.IS_BLOCK_FORMAT);
      // this check will be unnecessary once we have an XmlElement Pkl class
      if (!(isBlockFormat instanceof Boolean)) {
        throw new VmExceptionBuilder().typeMismatch(value, BaseModule.getBooleanClass()).build();
      }

      writeXmlElement((String) name, (VmMapping) attributes, value, (Boolean) isBlockFormat, true);
    }

    private boolean isXmlInline(Object value) {

View on GitHub (pinned to f3efcbfc9b)

Solutions

  1. Set `name` to a String literal or stringified expression (e.g. `name = "book"`)
  2. Explicitly convert: `name = id.toString()`
  3. Validate that all element objects you feed to the XML renderer have String `name`
  4. Use the pkl-xml library's `XmlElement` helpers instead of ad-hoc Dynamics

Example fix

// before
name = 42
// after
name = "42"
Defensive patterns

Strategy: type-guard

Validate before calling

name: String
assertion: name is String

Type guard

function isValidXmlElement(d: Dynamic): Boolean = d.name is String && d.attributes is Mapping && d.isBlockFormat is Boolean

Prevention

When it happens

Trigger: An object with `name`/`attributes`/`content` members rendered as XML where `name` evaluates to a non-String (e.g. an Int, Identifier, or another object).

Common situations: Hand-rolled XML element objects where `name` was accidentally assigned a number or expression returning non-String; typo'd member names causing fallback reads.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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