{"record":{"id":"48fa1a0e4fcca890","repo":"apple/pkl","slug":"typemismatch-48fa1a","errorCode":null,"errorMessage":"typeMismatch","messagePattern":"typeMismatch","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/ast/lambda/ApplyVmFunction1Node.java","lineNumber":45,"sourceCode":"import org.pkl.core.runtime.VmCollection;\nimport org.pkl.core.runtime.VmFunction;\n\n@NodeChild(\"functionNode\")\n@NodeChild(\"argumentNode\")\npublic abstract class ApplyVmFunction1Node extends ExpressionNode {\n  public abstract Object execute(VmFunction function, Object arg1);\n\n  public static ApplyVmFunction1Node create() {\n    //noinspection ConstantConditions\n    return ApplyVmFunction1NodeGen.create(null, null);\n  }\n\n  public final boolean executeBoolean(VmFunction function, Object arg1) {\n    var result = execute(function, arg1);\n    if (result instanceof Boolean b) return b;\n\n    CompilerDirectives.transferToInterpreter();\n    throw exceptionBuilder().typeMismatch(result, BaseModule.getBooleanClass()).build();\n  }\n\n  public final String executeString(VmFunction function, Object arg1) {\n    var result = execute(function, arg1);\n    if (result instanceof String string) return string;\n\n    CompilerDirectives.transferToInterpreter();\n    throw exceptionBuilder().typeMismatch(result, BaseModule.getStringClass()).build();\n  }\n\n  public final Long executeInt(VmFunction function, Object arg1) {\n    var result = execute(function, arg1);\n    if (result instanceof Long l) return l;\n\n    CompilerDirectives.transferToInterpreter();\n    throw exceptionBuilder().typeMismatch(result, BaseModule.getIntClass()).build();\n  }\n","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/ast/lambda/ApplyVmFunction1Node.java#L27-L63","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before (pkl)\nnums.filter((n) -> n * 2)          // returns Int -> typeMismatch (expected Boolean)\n\n// after\nnums.filter((n) -> n % 2 == 0)     // returns Boolean","handlingStrategy":"type-guard","validationCode":"// pkl\nassert(functionResult is Boolean, \"predicate must return a Boolean\")","typeGuard":"function isBooleanResult(f: (Any) -> Any, x: Any): Boolean = f(x) is Boolean","tryCatchPattern":null,"preventionTips":["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."],"tags":["pkl","type-mismatch","function-result","boolean"],"backgroundTag":"type-mismatch","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}