apple/pkl · error
typeMismatch
Error message
typeMismatch
What it means
Pkl's internal `ApplyVmFunction1Node.executeBoolean` calls a single-argument Pkl function and requires the result to be a Boolean. If the function returns anything else, it throws a `typeMismatch` error against `base::Boolean`. This guards internal call sites (such as predicate/result evaluation in `result`/`eval` paths) that assume a Boolean return value.
Solutions
- Inspect the function passed at the error location and ensure its body produces a Boolean (`true`/`false` or a comparison).
- If the function can yield null or other values, wrap the final expression so every branch returns a Boolean.
- If you intended a transformation rather than a predicate, use the appropriate API (map-style call) instead of one expecting a Boolean result.
- Add an explicit type assertion on the function's return expression to surface the mismatch closer to its source.
Example fix
// before (pkl) nums.filter((n) -> n * 2) // returns Int -> typeMismatch (expected Boolean) // after nums.filter((n) -> n % 2 == 0) // returns Boolean
Defensive patterns
Strategy: type-guard
Validate before calling
// pkl assert(functionResult is Boolean, "predicate must return a Boolean")
Type guard
function isBooleanResult(f: (Any) -> Any, x: Any): Boolean = f(x) is Boolean
Prevention
- Write predicates as direct comparisons (`x == y`, `x % 2 == 0`) so the result type is visibly Boolean.
- Never leave a predicate's last expression as a computed value or a nullable lookup.
When it happens
Trigger: A single-argument function is invoked through a code path that expects a Boolean result (executeBoolean, reached from `result`/`eval` evaluation) but the function body evaluates to a non-Boolean value, e.g. a lambda used as a predicate returns Int, String, or null.
Common situations: Passing a lambda that returns a number or string where a Boolean predicate is required; a function whose last expression is conditional and one branch yields null; using a mapping function instead of a filter/predicate function; typos like returning the compared values instead of the comparison.
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
- typeMismatch
- typeMismatch
- typeMismatch
- cannotExportValue
- cannotFlattenCollectionWithNonCollectionElement
AI-assisted analysis of apple/pkl@f3efcbfc9b (2026-09-08).
Data as JSON: /api/errors/48fa1a0e4fcca890.
Report an issue: GitHub.
Appendix: source
Thrown at pkl-core/src/main/java/org/pkl/core/ast/lambda/ApplyVmFunction1Node.java:45
import org.pkl.core.runtime.VmCollection;
import org.pkl.core.runtime.VmFunction;
@NodeChild("functionNode")
@NodeChild("argumentNode")
public abstract class ApplyVmFunction1Node extends ExpressionNode {
public abstract Object execute(VmFunction function, Object arg1);
public static ApplyVmFunction1Node create() {
//noinspection ConstantConditions
return ApplyVmFunction1NodeGen.create(null, null);
}
public final boolean executeBoolean(VmFunction function, Object arg1) {
var result = execute(function, arg1);
if (result instanceof Boolean b) return b;
CompilerDirectives.transferToInterpreter();
throw exceptionBuilder().typeMismatch(result, BaseModule.getBooleanClass()).build();
}
public final String executeString(VmFunction function, Object arg1) {
var result = execute(function, arg1);
if (result instanceof String string) return string;
CompilerDirectives.transferToInterpreter();
throw exceptionBuilder().typeMismatch(result, BaseModule.getStringClass()).build();
}
public final Long executeInt(VmFunction function, Object arg1) {
var result = execute(function, arg1);
if (result instanceof Long l) return l;
CompilerDirectives.transferToInterpreter();
throw exceptionBuilder().typeMismatch(result, BaseModule.getIntClass()).build();
}
View on GitHub (pinned to f3efcbfc9b)