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
- Set `name` to a String literal or stringified expression (e.g. `name = "book"`)
- Explicitly convert: `name = id.toString()`
- Validate that all element objects you feed to the XML renderer have String `name`
- 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
- Always define `name` as a String in element-like Dynamics
- Use the pkl-xml library's element builders
- Add module-level assertions validating element shape
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
- elementNotSupportedHere
- type mismatch: value is not of type Dynamic
- notASubclassOfTyped
- Error converting property `%s` in Pkl object of type `%s` to
- The top-level value of a YAML stream must have type `Collect
AI-assisted analysis of apple/pkl@f3efcbfc9b (2026-09-08).
Data as JSON: /api/errors/cf7a5d17001f8a87.
Report an issue: GitHub.